Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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# AddServiceEndpoint抛出的密钥为空?_C#_.net_Wcf_Endpoint - Fatal编程技术网

C# AddServiceEndpoint抛出的密钥为空?

C# AddServiceEndpoint抛出的密钥为空?,c#,.net,wcf,endpoint,C#,.net,Wcf,Endpoint,使用ServiceHost.AddServiceEndpoint添加自定义ProtoEndpointBehavior时,出现以下异常: System.ArgumentNullException:值不能为null。参数名称: 在System.Collections.Generic.Dictionary2.FindEntry(TKey)处键入 at System.Collections.Generic.Dictionary2.ContainsKey(TKey)at System.ServiceMod

使用ServiceHost.AddServiceEndpoint添加自定义ProtoEndpointBehavior时,出现以下异常:

System.ArgumentNullException:值不能为null。参数名称: 在System.Collections.Generic.Dictionary
2.FindEntry(TKey)处键入
at System.Collections.Generic.Dictionary
2.ContainsKey(TKey)at System.ServiceModel.ServiceHostBase.ImplementedContractsContractResolver.ResolveContract(字符串 合约名称)在 System.ServiceModel.ServiceHostBase.ServiceAndBehaviorsContractResolver.ResolveContract(字符串 合约名称)在 System.ServiceModel.Description.ConfigLoader.LookupContractForStandardEndpoint(字符串 合同名称、字符串(服务名称)位于 System.ServiceModel.Description.ConfigLoader.LookupContract(字符串 合同名称、字符串(服务名称)位于 System.ServiceModel.ServiceHostBase.AddServiceEndpoint(ServiceEndpoint 端点)在 My.Service.Business.ServiceHandler.StartService(类型serviceType, 中的字符串uri、SecureConnectionSettings(SecureConnectionSettings) C:\My\Produkter\My Utveckling\Solution\My.Service.Business\ServiceHandler.cs:第150行

这是代码的样子:

ServiceHost serviceHost = null;
Console.WriteLine("Creating service " + serviceType.FullName);
serviceHost = new MyServiceHost(serviceType, new Uri(uri));

var endPointAddress = "";

HttpBindingBase binding = null;
if (secureConnectionSettings != null && secureConnectionSettings.Enabled)
{
    Console.WriteLine("Setting certificates");
    X509Store store = new X509Store(secureConnectionSettings.CertificateStore, secureConnectionSettings.CertificateLocation);
    store.Open(OpenFlags.ReadOnly);
    X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, secureConnectionSettings.Thumbprint, true);
    store.Close();

    if (certs.Count > 0)
        serviceHost.Credentials.ServiceCertificate.SetCertificate(secureConnectionSettings.CertificateLocation,
                                                                secureConnectionSettings.CertificateStore,
                                                                X509FindType.FindByThumbprint,
                                                                secureConnectionSettings.Thumbprint);
    else
        throw new Exception("Could not finde certificate with thumbprint " + secureConnectionSettings.Thumbprint);

    endPointAddress = uri + "/BinaryHttpsProto";
    binding = CreateNetHttpsBinding(secureConnectionSettings);
}
else
{
    endPointAddress = uri + "/BinaryHttpProto";
    binding = CreateNetHttpBinding();
}

var endpoint = new System.ServiceModel.Description.ServiceEndpoint(new System.ServiceModel.Description.ContractDescription(typeof(IMyClientService).FullName), 
    binding, 
    new EndpointAddress(endPointAddress));

endpoint.EndpointBehaviors.Add(new ProtoBuf.ServiceModel.ProtoEndpointBehavior());
serviceHost.AddServiceEndpoint(endpoint);

Console.WriteLine("Starting service...");
serviceHost.Open();
Console.WriteLine("Service started successfully (" + uri + ")");
return serviceHost;
我在配置文件中设置如下:

  <endpointBehaviors>
    <behavior name="protoEndpointBehavior">
      <protobuf />
    </behavior>
  </endpointBehaviors>

现在我需要在代码中添加它

出什么问题了?

这是一个问题,有解决办法

获取ContractDescription实例时,请使用静态方法而不是直接使用ContractDescription()构造函数:

var endpoint = new System.ServiceModel.Description.ServiceEndpoint(System.ServiceModel.Description.ContractDescription.GetContract(typeof(IMyClientService)), 
    binding, 
    new EndpointAddress(endPointAddress));

我能够重现您的问题,此解决方法对我有效。

您看到异常的原因是
ServiceEndpoint
假设您使用配置文件。它试图检索和解析您的合同、绑定和地址,因为您没有使用配置文件,所以这些合同、绑定和地址不存在

(见备注)

当端点的绑定和地址为 在配置中提供

反对的方式有很多:

如果您确实坚持以编程方式创建
ContractDescription
,则需要定义以下内容:

  • 合同描述
  • 操作说明
  • 消息描述
  • 操作行为属性
  • 还有其他东西
或者你也可以作为一个踢球的人

你试过CodeFuller的答案吗?他的回答可以让你的生活更轻松。我做了一个快速检查,它的工作

class Program
{
    static void Main()
    {
        var baseAddress = new Uri("http://localhost:8080/ProtoBuf");

        using (var serviceHost = new ServiceHost(typeof(ProtoBufService), baseAddress))
        {
            var endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IProtoBufService)),
                new NetHttpBinding(),
                new EndpointAddress(baseAddress));

            endpoint.EndpointBehaviors.Add(new ProtoEndpointBehavior());
            serviceHost.AddServiceEndpoint(endpoint);

            serviceHost.Open();
            Console.ReadKey();
        }
    }
}
不幸的是,我无法附加“imgur有问题”的图像,但它可以在我的web浏览器上工作。虽然我还没有测试使用客户端应用程序,但我想你已经接近解决方案了


提示:不要混合使用配置文件和代码,因为它很难维护。“您可能知道”祝您好运:)

您发布的代码中的哪一行抛出了该错误?@FeryalBadili它是在endpoint.EndpointBehaviors.Add行上抛出的。谢谢!是的,CodeFuller似乎解决了这个问题,但我仍在检查它是否真的解决了。
class Program
{
    static void Main()
    {
        var baseAddress = new Uri("http://localhost:8080/ProtoBuf");

        using (var serviceHost = new ServiceHost(typeof(ProtoBufService), baseAddress))
        {
            var endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IProtoBufService)),
                new NetHttpBinding(),
                new EndpointAddress(baseAddress));

            endpoint.EndpointBehaviors.Add(new ProtoEndpointBehavior());
            serviceHost.AddServiceEndpoint(endpoint);

            serviceHost.Open();
            Console.ReadKey();
        }
    }
}