作成中に WCF サービスにオブジェクトを渡そうとしています - 「MasOperationsService」。しかし、エラーが発生し、その理由がわかりません。
ここからこのコードを試しています...
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MasOperationsService : IMasOperations
{
public MasOperationsService()
: this("INVALID")
{
throw new InvalidOperationException("This should never be called");
}
public MasOperationsService(string name)
{
}
//public CoAuthorSearchResult ExtractCoAuthorsFromAuthor(long AuthorCellId, uint LevelsToExtract)
//{
// //throw new NotImplementedException("Running This At Proxy, This should now query Slaves!!");
// return new CoAuthorSearchResult();
//}
}
public class MyInstanceProvider : IInstanceProvider
{
public object GetInstance(InstanceContext instanceContext, Message message)
{
string name = message.Headers.GetHeader<string>("Name", "http://my.namespace");
if (name != null)
{
return new MasOperationsService("Service " + name);
}
else
{
return new MasOperationsService("Service with no name");
}
}
public object GetInstance(InstanceContext instanceContext)
{
return new MasOperationsService("Service with no name");
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
}
}
public class MyServiceBehavior : IServiceBehavior
{
MyInstanceProvider myProvider = new MyInstanceProvider();
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { }
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher ed in cd.Endpoints)
{
ed.DispatchRuntime.InstanceProvider = this.myProvider;
}
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { }
}
MasOperationsService() はサービス クラスです。クライアント コードは LaunchWcfService() です。
public void LaunchWcfService()
{
string baseAddress = "http://localhost:8733/Design_Time_Addresses/MASService/Service1";
ServiceHost host = new ServiceHost(typeof(MasOperationsService), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(IMasOperations), GetBinding(), "");
host.Description.Behaviors.Add(new MyServiceBehavior());
host.Open();
Console.WriteLine("Host opened");
ChannelFactory<IMasOperations> factory = new ChannelFactory<IMasOperations>(GetBinding(), new EndpointAddress(baseAddress));
IMasOperations proxy = factory.CreateChannel();
using (OperationContextScope scope = new OperationContextScope((IContextChannel)proxy))
{
OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Name", "http://my.namespace", "Name 1"));
//Console.WriteLine(proxy.Hello("foo"));
OperationContext.Current.OutgoingMessageHeaders.RemoveAll("Name", "http://my.namespace");
OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Name", "http://my.namespace", "Name 2"));
//Console.WriteLine(proxy.Hello("bar"));
}
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
static Binding GetBinding()
{
BasicHttpBinding result = new BasicHttpBinding();
return result;
}