WCF服务 - 初始化问题

时间:2012-08-24 15:24:20

标签: c# wcf initialization

我目前正在尝试创建一个WCF服务,然后从commandLine应用程序托管它。在托管服务期间,我尝试使用名为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)。它被解雇了。

1 个答案:

答案 0 :(得分: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()。