Pau*_*ney 9 c# https soap servicestack soap1.2
我有一个ServiceStack基于Soap客户端,它可以正常运行HTTP,但是当我尝试使用HTTPS时,它给了我这个错误
ServiceStack.WebServiceException: The provided URI scheme 'https' is invalid; ex
pected 'http'.
Parameter name: via ---> System.ArgumentException: The provided URI scheme 'http
s' is invalid; expected 'http'.
Parameter name: via
at System.ServiceModel.Channels.HttpChannelFactory`1.ValidateCreateChannelPar
ameters(EndpointAddress remoteAddress, Uri via)
at System.ServiceModel.Channels.HttpChannelFactory`1.OnCreateChannelCore(Endp
ointAddress remoteAddress, Uri via)
at System.ServiceModel.Channels.ChannelFactoryBase`1.InternalCreateChannel(En
dpointAddress address, Uri via)
at System.ServiceModel.Channels.ServiceChannelFactory.ServiceChannelFactoryOv
erRequest.CreateInnerChannelBinder(EndpointAddress to, Uri via)
at System.ServiceModel.Channels.ServiceChannelFactory.CreateServiceChannel(En
dpointAddress address, Uri via)
at System.ServiceModel.Channels.ServiceChannelFactory.CreateChannel(Type chan
nelType, EndpointAddress address, Uri via)
at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address
, Uri via)
at System.ServiceModel.ClientBase`1.CreateChannel()
at System.ServiceModel.ClientBase`1.CreateChannelInternal()
at System.ServiceModel.ClientBase`1.get_Channel()
at ServiceStack.WcfServiceClient.Send(Message message)
at ServiceStack.WcfServiceClient.Send[T](Object request)
--- End of inner exception stack trace ---
at ServiceStack.WcfServiceClient.Send[T](Object request)
at ElectronicServiceInterface.ESIClient.Login()
Run Code Online (Sandbox Code Playgroud)
我使用的是自签名证书,但我可以使用curl成功调用我的Soap服务.这对我来说表明问题出在客户端.
我的代码沿袭了
using (var client = new Soap12ServiceClient(Uri))
{
var request = new MyRequestObject { Username = Username, Password = Password };
var response = client.Send<MyResponseObject>(request);
}
Run Code Online (Sandbox Code Playgroud)
以上显示了示例请求/响应DTO类的用法.
如何在不使用配置文件的情况下使用HTTPS,只需要代码,我该怎么做?
我相信你的问题在你的配置文件中,尝试添加到绑定标记.关于这个ServiceStack.IntegrationTests你应该有
<bindings>
<basicHttpBinding>
<binding name="Endpoint_BasicHttpBinding" />
</basicHttpBinding>
<wsHttpBinding>
<binding name="Endpoint_WsHttpBinding">
<security mode="None">
<transport clientCredentialType="None" />
<message establishSecurityContext="false" />
</security>
</binding>
</wsHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)
但在此测试中,它仅配置为在HTTP而非HTTPS上工作,因此您所要做的就是更改<transport clientCredentialType="None" />为<transport clientCredentialType="transport" />
配置文件的问题在于它并不总是简单明了,有时甚至不切实际。在此之上,ServiceStack 提倡无配置编码模型。
我成功地使用以下 SSL 中立客户端实现了所需的功能:
公共类 SoapServiceClient :Soap12ServiceClient
{
公共 SoapServiceClient(字符串 uri) : 基(uri)
{
if (uri.StartsWithIgnoreCase("https://"))
{
var 绑定 = (WSHttpBinding)base.Binding;
绑定.安全.模式=安全模式.传输;
}
}
}