C# 具有服务结构的SOAP—Https和Http绑定
我目前正在开发一个service fabric应用程序,该应用程序将公开另一个应用程序将使用的soap侦听器 我一直在说一个错误 找不到与的https方案匹配的基址 具有绑定CustomBinding的端点。注册基址方案 是[] 下面是CreateServiceInstanceListener方法C# 具有服务结构的SOAP—Https和Http绑定,c#,wcf,azure-service-fabric,C#,Wcf,Azure Service Fabric,我目前正在开发一个service fabric应用程序,该应用程序将公开另一个应用程序将使用的soap侦听器 我一直在说一个错误 找不到与的https方案匹配的基址 具有绑定CustomBinding的端点。注册基址方案 是[] 下面是CreateServiceInstanceListener方法 protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners() {
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
var serviceInstanceListers = new List<ServiceInstanceListener>()
{
new ServiceInstanceListener(context =>
{
return CreateSoapListener(context);
})
,
new ServiceInstanceListener(context =>
{
return CreateSoapHTTPSListener(context);
}),
};
return serviceInstanceListers;
}
private static ICommunicationListener CreateSoapHTTPSListener(StatelessServiceContext context)
{
string host = context.NodeContext.IPAddressOrFQDN;
var endpointConfig = context.CodePackageActivationContext.GetEndpoint("SecureServiceEndpoint");
int port = endpointConfig.Port;
string scheme = endpointConfig.Protocol.ToString();
string uri = string.Format(CultureInfo.InvariantCulture, "{0}://{1}:{2}/MyService/", scheme, host, port);
var listener = new WcfCommunicationListener<IServiceInterface>(
serviceContext: context,
wcfServiceObject: new Service(),
listenerBinding: new BasicHttpsBinding(BasicHttpsSecurityMode.Transport),
address: new EndpointAddress(uri)
);
// Check to see if the service host already has a ServiceMetadataBehavior
ServiceMetadataBehavior smb = listener.ServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
{
smb = new ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
smb.HttpsGetEnabled = true;
smb.HttpsGetUrl = new Uri(uri);
listener.ServiceHost.Description.Behaviors.Add(smb);
}
return listener;
}
private static ICommunicationListener CreateSoapListener(StatelessServiceContext context)
{
string host = context.NodeContext.IPAddressOrFQDN;
var endpointConfig = context.CodePackageActivationContext.GetEndpoint("ServiceEndpoint");
int port = endpointConfig.Port;
string scheme = endpointConfig.Protocol.ToString();
string uri = string.Format(CultureInfo.InvariantCulture, "{0}://{1}:{2}/MyService/", scheme, host, port);
var listener = new WcfCommunicationListener<IServiceInterface>(
serviceContext: context,
wcfServiceObject: new Service(),
listenerBinding: new BasicHttpBinding(BasicHttpSecurityMode.None),
address: new EndpointAddress(uri)
);
// Check to see if the service host already has a ServiceMetadataBehavior
ServiceMetadataBehavior smb = listener.ServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
{
smb = new ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
smb.HttpGetEnabled = true;
smb.HttpGetUrl = new Uri(uri);
listener.ServiceHost.Description.Behaviors.Add(smb);
}
return listener;
}
受保护的重写IEnumerable CreateServiceInstanceListeners()
{
var serviceInstanceListers=新列表()
{
新服务InstanceListener(上下文=>
{
返回CreateSoapListener(上下文);
})
,
新服务InstanceListener(上下文=>
{
返回CreateSoapHTTPSListener(上下文);
}),
};
返回服务实例列表;
}
专用静态ICommunicationListener CreateSoapHTTPSListener(无状态ServiceContext上下文)
{
字符串host=context.NodeContext.IPAddressOrFQDN;
var endpointConfig=context.CodePackageActivationContext.GetEndpoint(“SecureServiceEndpoint”);
int port=endpointConfig.port;
string scheme=endpointConfig.Protocol.ToString();
string uri=string.Format(CultureInfo.InvariantCulture,“{0}://{1}:{2}/MyService/”,scheme,host,port);
var listener=新的WcfCommunicationListener(
serviceContext:context,
wcfServiceObject:新服务(),
listenerBinding:新的BasicHttpsBinding(BasicHttpsSecurityMode.Transport),
地址:新端点地址(uri)
);
//检查服务主机是否已具有ServiceMetadataBehavior
ServiceMetadataBehavior smb=listener.ServiceHost.Description.Behaviors.Find();
//如果没有,请添加一个
如果(smb==null)
{
smb=新的ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion=PolicyVersion.Policy15;
smb.HttpsGetEnabled=true;
smb.HttpsGetUrl=新Uri(Uri);
listener.ServiceHost.Description.Behaviors.Add(smb);
}
返回侦听器;
}
专用静态ICommunicationListener CreateSoapListener(无状态ServiceContext上下文)
{
字符串host=context.NodeContext.IPAddressOrFQDN;
var endpointConfig=context.CodePackageActivationContext.GetEndpoint(“ServiceEndpoint”);
int port=endpointConfig.port;
string scheme=endpointConfig.Protocol.ToString();
string uri=string.Format(CultureInfo.InvariantCulture,“{0}://{1}:{2}/MyService/”,scheme,host,port);
var listener=新的WcfCommunicationListener(
serviceContext:context,
wcfServiceObject:新服务(),
listenerBinding:新的BasicHttpBinding(BasicHttpSecurityMode.None),
地址:新端点地址(uri)
);
//检查服务主机是否已具有ServiceMetadataBehavior
ServiceMetadataBehavior smb=listener.ServiceHost.Description.Behaviors.Find();
//如果没有,请添加一个
如果(smb==null)
{
smb=新的ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion=PolicyVersion.Policy15;
smb.HttpGetEnabled=true;
smb.HttpGetUrl=新Uri(Uri);
listener.ServiceHost.Description.Behaviors.Add(smb);
}
返回侦听器;
}
这是app.config(很抱歉,如果有无用的条目,我是从现有的WCF应用程序复制的)
我更新了我的CreateSoapHTTPSListener方法ad,下面是t的样子:
private static ICommunicationListener CreateSoapHTTPSListener(StatelessServiceContext context)
{
string host = context.NodeContext.IPAddressOrFQDN;
var endpointConfig = context.CodePackageActivationContext.GetEndpoint("SecureServiceEndpoint");
int port = endpointConfig.Port;
string scheme = endpointConfig.Protocol.ToString();
var binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.MaxReceivedMessageSize = 1073741824;
string uri = ConfigurationManager.AppSettings.Get("ProductManufacturerService");
Tools.TraceMessage(uri);
var listener = new WcfCommunicationListener<IProductServiceV20161>(
serviceContext: context,
wcfServiceObject: new ProductServiceManufacturer(),
listenerBinding: binding,
address: new EndpointAddress(uri)
);
listener.ServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, ConfigurationManager.AppSettings.Get("ServiceCertificateThumbprint"));
listener.ServiceHost.Credentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, ConfigurationManager.AppSettings.Get("ClientCertificateThumbprint"));
// Check to see if the service host already has a ServiceMetadataBehavior
ServiceMetadataBehavior smb = listener.ServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
{
smb = new ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
smb.HttpsGetEnabled = true;
smb.HttpGetEnabled = false;
smb.HttpsGetUrl = new Uri(uri);
listener.ServiceHost.Description.Behaviors.Add(smb);
}
return listener;
}
专用静态ICommunicationListener CreateSoapHTTPSListener(无状态ServiceContext上下文)
{
字符串host=context.NodeContext.IPAddressOrFQDN;
var endpointConfig=context.CodePackageActivationContext.GetEndpoint(“SecureServiceEndpoint”);
int port=endpointConfig.port;
string scheme=endpointConfig.Protocol.ToString();
var binding=新的BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
binding.Security.Transport.ClientCredentialType=HttpClientCredentialType.None;
binding.MaxReceivedMessageSize=1073741824;
字符串uri=ConfigurationManager.AppSettings.Get(“ProductManufacturerService”);
跟踪消息(uri);
var listener=新的WcfCommunicationListener(
serviceContext:context,
wcfServiceObject:new ProductServiceManufacturer(),
listenerBinding:绑定,
地址:新端点地址(uri)
);
listener.ServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine,StoreName.My,X509FindType.FindByThumbprint,ConfigurationManager.AppSettings.Get(“ServiceCertificateThumbprint”);
listener.ServiceHost.Credentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine,StoreName.My,X509FindType.FindByThumbprint,ConfigurationManager.AppSettings.Get(“ClientCertificateThumbprint”);
//检查服务主机是否已具有ServiceMetadataBehavior
ServiceMetadataBehavior smb=listener.ServiceHost.Description.Behaviors.Find();
//如果没有,请添加一个
如果(smb==null)
{
smb=新的ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion=PolicyVersion.Policy15;
smb.HttpsGetEnabled=true;
smb.HttpGetEnabled=false;
smb.HttpsGetUrl=新Uri(Uri);
listener.ServiceHost.Description.Behaviors.Add(smb);
}
返回侦听器;
}
然后我犯了一个错误,说:
该服务包含多个具有不同契约描述的ServiceEndpoint,每个契约描述都有Name='IPProductServiceV20161'和Namespace='namespaceurl/'
我想这是因为服务端点有两种定义
private static ICommunicationListener CreateSoapHTTPSListener(StatelessServiceContext context)
{
string host = context.NodeContext.IPAddressOrFQDN;
var endpointConfig = context.CodePackageActivationContext.GetEndpoint("SecureServiceEndpoint");
int port = endpointConfig.Port;
string scheme = endpointConfig.Protocol.ToString();
var binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.MaxReceivedMessageSize = 1073741824;
string uri = ConfigurationManager.AppSettings.Get("ProductManufacturerService");
Tools.TraceMessage(uri);
var listener = new WcfCommunicationListener<IProductServiceV20161>(
serviceContext: context,
wcfServiceObject: new ProductServiceManufacturer(),
listenerBinding: binding,
address: new EndpointAddress(uri)
);
listener.ServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, ConfigurationManager.AppSettings.Get("ServiceCertificateThumbprint"));
listener.ServiceHost.Credentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, ConfigurationManager.AppSettings.Get("ClientCertificateThumbprint"));
// Check to see if the service host already has a ServiceMetadataBehavior
ServiceMetadataBehavior smb = listener.ServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
{
smb = new ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
smb.HttpsGetEnabled = true;
smb.HttpGetEnabled = false;
smb.HttpsGetUrl = new Uri(uri);
listener.ServiceHost.Description.Behaviors.Add(smb);
}
return listener;
}
listenerBinding: new BasicHttpsBinding(BasicHttpsSecurityMode.Transport)
listenerBinding: binding
var binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport)
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
listener.ServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, "Certificate Thumbprint Here");
<service name="MyApp.ProductServiceManufacturer" behaviorConfiguration="ManufacturerBehaviour">
<endpoint address="https://serverurl:8088/IServiceInterface/Service.svc" name="ManufacturerProductService" binding="customBinding" bindingConfiguration="HubBinding" contract="MyApp.IProductServiceV20161"/>
</service>
<service name="MyApp.ProductServiceManufacturer" behaviorConfiguration="ManufacturerBehaviour">
<endpoint address="Service.svc" name="ManufacturerProductService" binding="customBinding" bindingConfiguration="HubBinding" contract="MyApp.IProductServiceV20161"/>
<host>
<baseAddresses>
<add baseAddress="https://serverurl:8088/IServiceInterface" />
</baseAddresses>
</host>
</service>
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
var serviceInstanceListers = new List<ServiceInstanceListener>()
{
new ServiceInstanceListener(context =>
{
//return CreateRestListener(context);
return CreateSoapHTTPSListener(context);
}),
};
return serviceInstanceListers;
}
private static ICommunicationListener CreateSoapHTTPSListener(StatelessServiceContext context)
{
var binding = new CustomBinding();
AsymmetricSecurityBindingElement assbe = (AsymmetricSecurityBindingElement)SecurityBindingElement.CreateMutualCertificateBindingElement(
MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10);
binding.Elements.Add(assbe);
binding.Elements.Add(new TextMessageEncodingBindingElement());
binding.Elements.Add(new HttpsTransportBindingElement());
// Extract the STS certificate from the certificate store.
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, ConfigurationManager.AppSettings.Get("ClientCertificateThumbprint"), false);
store.Close();
var identity = EndpointIdentity.CreateX509CertificateIdentity(certs[0]);
string uri = ConfigurationManager.AppSettings.Get("ServiceUri");
var listener = new WcfCommunicationListener<IService>(
serviceContext: context,
wcfServiceObject: new Service(),//where service implements IService
listenerBinding: binding,
address: new EndpointAddress(new Uri(uri), identity)
);
listener.ServiceHost.Credentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, ConfigurationManager.AppSettings.Get("ServiceCertificateThumbprint"));
listener.ServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, ConfigurationManager.AppSettings.Get("ClientCertificateThumbprint"));
// Check to see if the service host already has a ServiceMetadataBehavior
ServiceMetadataBehavior smb = listener.ServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
{
smb = new ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy12;
smb.HttpsGetEnabled = true;
smb.HttpGetEnabled = false;
smb.HttpsGetUrl = new Uri(uri);
listener.ServiceHost.Description.Behaviors.Add(smb);
}
return listener;
}