C# WCF服务-初始化问题
我目前正在尝试创建一个WCF服务,然后从命令行应用程序托管它。在托管服务期间,我试图用一个名为MyProxy的对象实例对其进行初始化。对服务的所有调用都应委托给MyProxy 我已经创建了一个服务并拨打了电话。但我无法在服务中初始化MyProxy实例。它总是空的。因此,当对服务进行任何调用时,我无法将其委托给代理 从最近两天开始,我一直在努力完成这项工作。我现在迷路了,不知道发生了什么。请帮忙C# WCF服务-初始化问题,c#,wcf,initialization,C#,Wcf,Initialization,我目前正在尝试创建一个WCF服务,然后从命令行应用程序托管它。在托管服务期间,我试图用一个名为MyProxy的对象实例对其进行初始化。对服务的所有调用都应委托给MyProxy 我已经创建了一个服务并拨打了电话。但我无法在服务中初始化MyProxy实例。它总是空的。因此,当对服务进行任何调用时,我无法将其委托给代理 从最近两天开始,我一直在努力完成这项工作。我现在迷路了,不知道发生了什么。请帮忙 public class MasOperationsService : IMasOperations
public class MasOperationsService : IMasOperations
{
//This MyProxy instance should be used to delegate all calls to service.
public MyProxy myProxyInstance;
public MasOperationsService()
{
myProxyInstance = null;
}
public MasOperationsService(MyProxy proxy)
{
myProxyInstance = proxy;
}
public CoAuthorSearchResult ExtractCoAuthorsFromAuthor(long authorCellId, uint levelsToExtract)
{
//The service will delegate the call to MyProxy.
//myProxyInstance is always null
return myProxyInstance.GetProxyData(...);
}
}
public class MyInstanceProvider : IInstanceProvider
{
public object GetInstance(InstanceContext instanceContext, Message message)
{
MyProxy name = message.Headers.GetHeader<MyProxy>("Name", "http://my.namespace");
if (name != null)
{
return new MasOperationsService(name);
}
return null;
}
public object GetInstance(InstanceContext instanceContext)
{
return new MasOperationsService(null);
}
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:iMasoOperations
{
//此MyProxy实例应用于将所有调用委托给服务。
公共MyProxy myProxyInstance;
公共服务
{
myProxyInstance=null;
}
公共MasOperationsService(MyProxy代理)
{
myProxyInstance=代理;
}
公共合著者搜索结果摘录合著者fromAuthor(长authorCellId,uint级别为摘录)
{
//该服务将把调用委托给MyProxy。
//myProxyInstance始终为空
返回myProxyInstance.GetProxyData(…);
}
}
公共类MyInstanceProvider:IInstanceProvider
{
公共对象GetInstance(InstanceContext InstanceContext,消息)
{
MyProxy名称=message.Headers.GetHeader(“名称”,”http://my.namespace");
if(name!=null)
{
返回新的MasOperationsService(名称);
}
返回null;
}
公共对象GetInstance(InstanceContext InstanceContext)
{
返回新的MasOperationsService(空);
}
public void ReleaseInstance(InstanceContext InstanceContext,对象实例)
{
}
}
公共类MyService行为:IServiceBehavior
{
MyInstanceProvider myProvider=新的MyInstanceProvider();
public void AddBindingParameters(ServiceDescription ServiceDescription,ServiceHostBase ServiceHostBase,集合终结点,BindingParameterCollection bindingParameters){}
公共无效ApplyDispatchBehavior(ServiceDescription ServiceDescription,ServiceHostBase ServiceHostBase)
{
foreach(serviceHostBase.ChannelDispatchers中的ChannelDispatcher cd)
{
foreach(在cd.Endpoints中终结)
{
ed.DispatchRuntime.InstanceProvider=this.myProvider;
}
}
}
公共无效验证(ServiceDescription ServiceDescription,ServiceHostBase ServiceHostBase){}
}
该服务通过以下方式启动:
class MyServiceLauncher
{
ServiceHost host;
IMasOperations proxy;
ChannelFactory<IMasOperations> factory;
public void StartService(MyProxy proxyInstance)
{
string baseAddress = "http://localhost:8730/Design_Time_Addresses/MASService/Service1";
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");
factory = new ChannelFactory<IMasOperations>(GetBinding(), new EndpointAddress(baseAddress));
proxy = factory.CreateChannel();
using (OperationContextScope scope = new OperationContextScope((IContextChannel)proxy))
{
OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Name", "http://my.namespace", proxyInstance));
}
}
public void ShutDownService()
{
((IClientChannel)proxy).Close();
factory.Close();
host.Close();
}
static Binding GetBinding()
{
BasicHttpBinding result = new BasicHttpBinding();
return result;
}
}
类MyServiceLauncher
{
服务主机;
IMasOperations代理;
渠道工厂;
public void StartService(MyProxy proxyInstance)
{
字符串基地址=”http://localhost:8730/Design_Time_Addresses/MASService/Service1";
主机=新服务主机(typeof(MasOperationsService),新Uri(baseAddress));
AddServiceEndpoint(typeof(IMasOperations),GetBinding(),“”);
host.Description.Behaviors.Add(新的MyServiceBehavior());
host.Open();
Console.WriteLine(“主机已打开”);
factory=newchannelfactory(GetBinding(),newendpointaddress(baseAddress));
proxy=factory.CreateChannel();
使用(OperationContextScope范围=新的OperationContextScope((IContextChannel)代理))
{
OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(“名称”)http://my.namespace“,proxyInstance”);
}
}
公共服务
{
((IClientChannel)代理).Close();
工厂关闭();
host.Close();
}
静态绑定GetBinding()
{
BasicHttpBinding结果=新的BasicHttpBinding();
返回结果;
}
}
我还将Debugger.Launch()放在几乎所有地方,只是为了看到它被初始化(服务的构造函数和IInstanceProvider中)。它不会被解雇。以下是缺失的类型。在服务中,还有一个电话 返回myProxyInstance.GetProxyData(…); 删除圆点将编译应用程序
[ServiceContract]
public interface IMasOperations
{
[OperationContract]
CoAuthorSearchResult ExtractCoAuthorsFromAuthor(long AuthorCellId, uint LevelsToExtract);
}
public class CoAuthorSearchResult
{ }
public class MyProxy
{
public CoAuthorSearchResult GetProxyData()
{
return new CoAuthorSearchResult();
}
}
@科巴克
要求提供显示调用GetInstance的位置的代码。我不确定我需要如何以及为什么这样做
目前,我只是在客户端创建了一个服务类的对象-MasOperationsClient,并调用方法extractCoauthorFromAuthor()。您能否提供一段代码,在其中调用
GetInstance(InstanceContext InstanceContext,Message Message)
MyInstanceProvider的方法。另外,提供契约定义也不错。我将您的代码粘贴到Visual Studio中,但我无法对其执行任何操作,因为它无法编译(MyProxy和其他类型未定义)。您正在请求帮助调试某些内容。如果你让我很容易做一个调试会话,我会的。否则你可能得不到答案。@kobac——我不知道我需要如何以及为什么这样做。目前,我只是在客户机上创建了一个服务类的对象——MasOperationsClient,并调用方法ExtractCoAuthorsFromAuthor()。@ErnieL——我已经回复了我的帖子,其中包含了我之前遗漏的类型。