2

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

在此 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);
    }
}
4

0 回答 0