0

我目前正在尝试创建 WCF 服务,然后从命令行应用程序托管它。在托管服务期间,我尝试使用名为 MyProxy 的对象实例对其进行初始化。对服务的所有调用都应委托给 MyProxy。

我创建了一项服务并拨打了电话。但我无法在服务中初始化 MyProxy 实例。它始终为空。因此,当对服务进行任何调用时,我无法将其委托给代理。

从最近两天开始,我一直在努力完成这项工作。我现在迷路了,不知道发生了什么。请帮忙。

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) { }
}  

该服务是使用启动的,

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;
    }
}

我还在几乎所有地方都放置了一个 Debugger.Launch(),只是为了看到它被初始化(Service 的构造函数和 IInstanceProvider 中的构造函数)。它不会被解雇。

4

1 回答 1

0

这是缺少的类型。同样在服务中,有一个调用 return 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();
    }
}

@kobac 要求提供一段代码来显示调用 GetInstance 的位置。我不确定如何以及为什么需要这样做。

目前我只是在客户端创建服务类的一个对象——MasOperationsClient 并调用方法 ExtractCoAuthorsFromAuthor()。

于 2012-08-24T18:27:58.660 回答