AddServiceEndpoint throws键为null?

时间:2017-02-19 13:47:12

标签: c# .net wcf endpoint

使用ServiceHost.AddServiceEndpoint添加自定义ProtoEndpointBehavior时,我得到以下异常:

  

System.ArgumentNullException:值不能为null。参数名称:   System.Collections.Generic.Dictionary 2.FindEntry(TKey key) at System.Collections.Generic.Dictionary上的键2.ContainsKey(TKey键)at   System.ServiceModel.ServiceHostBase.ImplementedContractsContractResolver.ResolveContract(字符串   contractName)at   System.ServiceModel.ServiceHostBase.ServiceAndBehaviorsContractResolver.ResolveContract(字符串   contractName)at   System.ServiceModel.Description.ConfigLoader.LookupContractForStandardEndpoint(字符串   contractName,String serviceName)at   System.ServiceModel.Description.ConfigLoader.LookupContract(字符串   contractName,String serviceName)at   System.ServiceModel.ServiceHostBase.AddServiceEndpoint(ServiceEndpoint   终点)   My.Service.Business.ServiceHandler.StartService(输入serviceType,   字符串uri,SecureConnectionSettings secureConnectionSettings)in   C:\我的\ Produkter \我的   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>

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

有什么问题?

2 个答案:

答案 0 :(得分:8)

它是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));

我能够重现您的问题,这个解决方法对我有用。

答案 1 :(得分:1)

您看到异常的原因是因为import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFTable; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.util.ArrayList; import java.util.List; public class ExcelReader { public static String[][] readDataExcel(String TableName) { String filePath = "src/test/resources/Testdata.xlsx"; try { Workbook workbook = new XSSFWorkbook(filePath); XSSFSheet sheet = (XSSFSheet) workbook.getSheet("UnitTest"); List<XSSFTable> tables = sheet.getTables(); for (XSSFTable table : tables) { String name = table.getName(); if (! name.equals(TableName)) { continue; } int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum(); System.out.println(rowCount); int columnCount = - 1; List<List<String>> data = new ArrayList<>(); for (int i = 1; i < rowCount + 1; i++) { Row row = sheet.getRow(i); int colNum = row.getLastCellNum(); if (columnCount == - 1) { columnCount = colNum; } List<String> rowData = new ArrayList<>(); for (int j = 0; j < colNum; j++) { Cell cell = row.getCell(j); DataFormatter formatter = new DataFormatter(); String var_name = formatter.formatCellValue(cell); if (var_name != null && ! var_name.trim().isEmpty()) { rowData.add(var_name); } } if (! rowData.isEmpty()) { data.add(rowData); } } workbook.close(); String[][] dataArray = new String[rowCount-1][columnCount]; int rowIndex = 0; for (List<String> row : data) { int colIndex = 0; for (String rowData : row) { dataArray[rowIndex][colIndex++] = rowData; } rowIndex++; } return dataArray; } System.err.println("Table name is incorrect"); return new String[][] {{}}; } catch (Exception e) { e.printStackTrace(); return new String[][] {{}}; } } } 假设您使用配置文件。它试图检索&amp;解决因为您没有使用配置文件而不存在的合同,绑定和地址。

ServiceEndpoint(见备注)

  

当端点的绑定和地址为时,使用此构造函数   在配置中提供。

create or retrieve a ContractDescription对象有多种方式:

ServiceEndpoint

如果您真的坚持以编程方式创建var contractUsingName = new ContractDescription("IProtoBufService"); var contractUsingNameWithNameSpace = new ContractDescription("IProtoBufService", "http://www.tempuri.org"); var contractUsingContractType = ContractDescription.GetContract(typeof(IProtoBufService)); var contractUsingContractTypeAndObjectImp = ContractDescription.GetContract(typeof(IProtoBufService), ProtoBufService); var contractUsingContractTypeAndObjectType = ContractDescription.GetContract(typeof(IProtoBufService), typeof(ProtoBufService)); ,则需要定义以下某些内容:

  • ContractDescription
  • OperationDescription
  • MessageDescription
  • OperationBehaviorAttribute
  • 和其他东西......

或者您可以将此blog作为踢球者。

您是否尝试过CodeFuller的答案?他的回答可以让你的生活更轻松。我做了一个快速检查,然后就可以了。

ContractDescription

很遗憾,我无法附加图片&#34; imgur&#34; 的错误,但它可以在我的网络浏览器上运行。虽然我还没有使用客户端应用程序进行测试,但我猜你已经接近解决方案。

提示:不要混用配置文件和代码,因为它很难维护。 &#34;你可能知道&#34; 祝你好运:)