Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Ninject、WCF&;自动发现_C#_Wcf_Ninject_Ninject Extensions - Fatal编程技术网

C# Ninject、WCF&;自动发现

C# Ninject、WCF&;自动发现,c#,wcf,ninject,ninject-extensions,C#,Wcf,Ninject,Ninject Extensions,我似乎无法让这三个一起工作 我用一种方法将其缩小为一个非常简单的服务: [System.ServiceModel.ServiceContract] public interface Icontract { [System.ServiceModel.OperationContract] void Ping(); } public class contract : Icontract { public void Ping() { } } 我有一个工厂,看起来像这样

我似乎无法让这三个一起工作

我用一种方法将其缩小为一个非常简单的服务:

[System.ServiceModel.ServiceContract]
public interface Icontract
{
    [System.ServiceModel.OperationContract]
    void Ping();
}

public class contract : Icontract
{
    public void Ping()
    { }
}
我有一个工厂,看起来像这样:

public class ServiceFactory
{
    private readonly IKernel _kernel;

    public ServiceFactory(IKernel kernel)
    {
        _kernel = kernel;
    }

    public NinjectServiceHost<T> GetService<T>()
    {
        return _kernel.Get<NinjectServiceHost<T>>();
    }
}
_kernel = new StandardKernel(new YourNinjectModule());
…探索号很好用。但是如果我像这样使用工厂

_tmp = new ServiceHost(typeof(ConsoleApplication1.contract));
_tmp.Open();
_tmp = _factory.GetService<ConsoleApplication1.contract>();
_tmp.Open();
\u tmp=\u factory.GetService();
_tmp.Open();
…该服务不再可被发现有关该服务的所有其他内容均按预期工作


有人对发现这样工作感到高兴,还是我做错了什么?

您在哪里设置绑定?在代码的某个地方,您需要使用
ServiceModule
初始化
内核
,如下所示:

public class ServiceFactory
{
    private readonly IKernel _kernel;

    public ServiceFactory(IKernel kernel)
    {
        _kernel = kernel;
    }

    public NinjectServiceHost<T> GetService<T>()
    {
        return _kernel.Get<NinjectServiceHost<T>>();
    }
}
_kernel = new StandardKernel(new YourNinjectModule());
然后在模块代码中:

public class YourNinjectModule: NinjectModule
{
    /// <summary>
    /// Loads the module into the kernel.
    /// </summary>
    public override void Load()
    {
        // ===========================================================
        //
        // Bind dependency injection.
        // Add entries in here for any new services or repositories.
        //
        // ===========================================================

        this.Bind<Icontract>().To<contract>();
    }
}
Unbind<IServiceBehavior>();
Bind<IServiceBehavior>().To<NinjectServiceBehaviorFixed>();
公共类YourNinjectModule:NinjectModule
{
/// 
///将模块加载到内核中。
/// 
公共覆盖无效负载()
{
// ===========================================================
//
//绑定依赖注入。
//在此处为任何新服务或存储库添加条目。
//
// ===========================================================
this.Bind().To();
}
}
这是Ninject.Extensions.Wcf.NinjectServiceBehavior类中的一个bug。 只需使用以下方法创建您自己的从IServiceBehavior接口派生的NinjectServiceBehaviorFixed类:

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
    foreach (EndpointDispatcher endpointDispatcher in serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>().SelectMany(channelDispatcher => (IEnumerable<EndpointDispatcher>) channelDispatcher.Endpoints))
    {
        if (endpointDispatcher.DispatchRuntime.InstanceProvider == null)
        {
            endpointDispatcher.DispatchRuntime.InstanceProvider = _instanceProviderFactory(serviceDescription.ServiceType);
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(_requestScopeCleanUp);
        }
   }
}
public void ApplyDispatchBehavior(ServiceDescription ServiceDescription,ServiceHostBase ServiceHostBase)
{
foreach(serviceHostBase.ChannelDispatchers.OfType()中的EndpointDispatcher EndpointDispatcher。SelectMany(channelDispatcher=>(IEnumerable)channelDispatcher.Endpoints))
{
if(endpointDispatcher.DispatcheRuntime.InstanceProvider==null)
{
endpointDispatcher.DispatchRuntime.InstanceProvider=\u instanceProviderFactory(serviceDescription.ServiceType);
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(_requestScopeCleanUp);
}
}
}
添加到您的模块代码中:

public class YourNinjectModule: NinjectModule
{
    /// <summary>
    /// Loads the module into the kernel.
    /// </summary>
    public override void Load()
    {
        // ===========================================================
        //
        // Bind dependency injection.
        // Add entries in here for any new services or repositories.
        //
        // ===========================================================

        this.Bind<Icontract>().To<contract>();
    }
}
Unbind<IServiceBehavior>();
Bind<IServiceBehavior>().To<NinjectServiceBehaviorFixed>();
Unbind();
绑定()到();

在ApplyDispatchBehavior方法中,我们刚刚添加了检查endpointDispatcher.DispatchRuntime.InstanceProvider是否为null,因为WS-Discovery使用定义的InstanceProvider创建了新的端点,而InstanceProvider不应被覆盖

我知道这一点。绑定配置正确。服务按预期工作。然而,这个发现并没有。我问这个问题是因为我很好奇你的绑定是什么。您请求的是具体类(
contract
)而不是接口(
Icontract
)。通常你会使用
Get
。通常你会。但是,ServiceHost&因此NinjectServiceHost只接受具体的类。如果在调用factory方法时指定
Icontract
,则会出现类似的错误。仍然找不到解决方案?我无法再对此进行测试,但听起来很有说服力