当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - WCF service - initialization issue

问题描述:

I am currently trying to create a WCF service and then host it from a commandLine application. During hosting of the service am trying to initialize it with an instance of an object called MyProxy. All calls to the service should delegate to MyProxy.

I have created a service and made calls. But I am not able to initialize the MyProxy instance in the service. It is always null. So when any calls to the service is made, I am not able to delegate it to the proxy.

I have been trying to get this work since last two days. Am lost now, not sure what is happening. Kindly help.

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

}

The service is launched using,

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;

}

}

I also put a Debugger.Launch() almost everywhere, just to see it being initialized (Both constructor of Service and in IInstanceProvider). It dosent get fired.

网友答案:

Here are the missing types. Also in the service, there is a call return myProxyInstance.GetProxyData(...); removing the dots compiles the application.

[ServiceContract]
public interface IMasOperations
{
    [OperationContract]
    CoAuthorSearchResult ExtractCoAuthorsFromAuthor(long AuthorCellId, uint LevelsToExtract);
}

public class CoAuthorSearchResult
{ }

public class MyProxy
{ 
    public CoAuthorSearchResult GetProxyData()
    {
        return new CoAuthorSearchResult();
    }
}

@kobac asked for a piece of code showing where GetInstance is being called. I am not sure how and why I need to do this.

Currently I just create an object of the service class - MasOperationsClient at the client and call the method ExtractCoAuthorsFromAuthor().

分享给朋友:
您可能感兴趣的文章:
随机阅读: