为什么WCF发现无法在WCF中使用自定义服务工厂主机?

时间:2012-06-12 11:30:03

标签: wcf

我在IIS托管的WCF服务中使用unity创建了一个自定义ServiceHostFactory,一个ServiceHost,一个ServiceBehavior和一个ServiceInstanceProvider用于DI。

此UDP发现停止工作后。 WCF如何创建ServiceDiscovery行为? UDPDiscoveryEndpoint在哪里创建?我是否在下面的代码中重写了一些行为?

public class ServiceHostFactory : System.ServiceModel.Activation.ServiceHostFactory
{
    protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {   
        return new ServiceHost(serviceType, baseAddresses);
    }
}

public class ServiceHost : System.ServiceModel.ServiceHost
{ ....
    protected override void OnOpen(TimeSpan timeout)
    {
        Description.Behaviors.Add(new ServiceBehavior());            
        base.OnOpen(timeout);
    }
}

public class ServiceBehavior : IServiceBehavior
{
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
        {
            var cd = cdb as ChannelDispatcher;
            if (cd != null)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {   
                    ed.DispatchRuntime.InstanceProvider = new ServiceInstanceProvider(serviceDescription.ServiceType);
                }
            }
        }
    }
}

public class ServiceInstanceProvider : IInstanceProvider
{...
    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return Container.Instance.Resolve(_serviceType);
    }
}

0 个答案:

没有答案