gpt4 book ai didi

c# - AddServiceEndpoint 抛出键为空?

转载 作者:可可西里 更新时间:2023-11-01 09:08:47 33 4
gpt4 key购买 nike

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

System.ArgumentNullException: Value cannot be null. Parameter name: key at System.Collections.Generic.Dictionary2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary
2.ContainsKey(TKey key) at System.ServiceModel.ServiceHostBase.ImplementedContractsContractResolver.ResolveContract(String contractName) at System.ServiceModel.ServiceHostBase.ServiceAndBehaviorsContractResolver.ResolveContract(String contractName) at System.ServiceModel.Description.ConfigLoader.LookupContractForStandardEndpoint(String contractName, String serviceName) at System.ServiceModel.Description.ConfigLoader.LookupContract(String contractName, String serviceName) at System.ServiceModel.ServiceHostBase.AddServiceEndpoint(ServiceEndpoint endpoint) at My.Service.Business.ServiceHandler.StartService(Type serviceType, String uri, SecureConnectionSettings secureConnectionSettings) in C:\My\Produkter\My Utveckling\Solution\My.Service.Business\ServiceHandler.cs:line 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>

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

怎么了?

最佳答案

这是一个known bug并且有解决方法。

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

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

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

关于c# - AddServiceEndpoint 抛出键为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42327988/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com