我在我的本地IIS服务器上运行了一个WCF服务.我已将其添加为C#Website Project的服务引用,并且它会添加正常并自动生成代理类.
但是,当我尝试调用任何服务合同时,我收到以下错误:
描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息: System.ServiceModel.ProtocolException:内容类型text/html; 所述响应消息不匹配的结合(;字符集= UTF-8应用/肥皂+ XML)的内容类型的字符集= UTF-8.如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法.响应的前1024个字节是:'function bredir(d,u,r,v,c){var w,h,wd,hd,bi; var b = false; var p = false; var s = [[ 300250,假],[250250,假],[240400,假],[336280,假],[180150,假],[468,60,假],[234,60,假],[88,31,假],[120,90,假],[120,60,假],[120240,假],[125125,假],[728,90,假],[160600,假],[120600,假] [300600,假],[300125,假],[530300,假],[190200,假],[470250,假],[720300,真],[500350,真],[550480,真]]; if(typeof(window.innerHeight)=='number'){h = window.innerHeight; w = window.innerWidth;} else if(typeof(document.body.offsetHeight)=='number'){h = document. body.offsetHeight; w = document.body.offsetWidth;} for(var i = 0; i
我还有一个控制台应用程序,它也与WCF服务通信,控制台应用程序能够调用方法,而不会出现此错误.
以下是我的配置文件的摘录.
WCF服务Web.Config:
<system.serviceModel>
<services>
<service name="ScraperService" behaviorConfiguration="ScraperServiceBehavior">
<endpoint address=""
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IScraperService"
contract="IScraperService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://example.com" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IScraperService"
bypassProxyOnLocal="false" transactionFlow="false" …Run Code Online (Sandbox Code Playgroud) 我需要创建一个wcf客户端来调用我无法控制的服务.
我得到了一个wsdl和一个有效的soapui项目.
该服务使用用户名/密码和x509证书.
UPDATE
我现在明白问题是什么,但我仍然不确定我需要采取哪些步骤来创建所需的消息,所以任何帮助都将非常感激.
我需要同时签署UsernameToken和SecurityTokenReference.
由于不再使用,我必须从此帖子中删除我必须创建自定义绑定的代码.我不再向绑定添加SecurityBindingElement,而是添加了一个将安全元素写入标头的新行为.
因此,通过继承SignedXml类,添加签名引用然后调用ComputeSignature以在Security头中创建Signature节点,从头开始创建安全节点.
您需要传递xml以登录SignedXml构造函数才能使其生效.传递UsernameToken没问题,这似乎是正确签名的.
问题是SecurityTokenReference仅在调用ComputeSignature()时创建,因此我无法为此元素添加签名引用,因为它在需要时不存在(在SignedXml的重写的GetIdElement方法中)在ComputeSignature()之前调用的)
我用来创建要插入Security头的签名块的代码如下
string certificatePath = System.Windows.Forms.Application.StartupPath + "\\" + "Certs\\sign-and- enc.p12";
XmlDocument xd = new XmlDocument();
xd.LoadXml(xml);
// Set Certificate
System.Security.Cryptography.X509Certificates.X509Certificate2 cert = new X509Certificate2(certificatePath, "password");
MySignedXml signedXml = new MySignedXml(xd);
signedXml.SigningKey = cert.PrivateKey;
// Create a new KeyInfo object.
KeyInfo keyInfo = new KeyInfo();
keyInfo.Id = "";
MemoryStream keyInfoStream = new MemoryStream();
XmlWriter keyInfoWriter = XmlWriter.Create(keyInfoStream);
WSSecurityTokenSerializer.DefaultInstance.WriteKeyIdentifierClause(keyInfoWriter, new LocalIdKeyIdentifierClause("token_reference", typeof(X509SecurityToken)));
keyInfoWriter.Flush();
keyInfoStream.Position = 0;
XmlDocument keyInfoDocument = new XmlDocument();
keyInfoDocument.Load(keyInfoStream); …Run Code Online (Sandbox Code Playgroud) 我正在使用.NET 4.0在IIS 7.5中托管WCF服务.我还有一个WPF应用程序,我用作使用Visual Studio 2010和.NET 4.0构建的客户端.我添加了我的服务引用,当我尝试调用函数时,我得到以下异常
内容类型application/xml; 响应消息的charset = utf-8与绑定的内容类型不匹配(application/soap + xml; charset = utf-8)
我能够在Web浏览器中导航到该服务,并且我的绑定在客户端和服务之间看起来是相同的(WsHttp绑定).
我知道有很多关于此错误的谷歌搜索结果,但它们似乎都不相关/帮助我的具体问题.我尝试安装非HTTP激活功能以及各种其他小技巧.有人能帮忙吗?谢谢
编辑,这是我的配置(他们很长)
客户
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ContentSoap"
closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="2147483647"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
<binding name="OrderSoap"
closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" …Run Code Online (Sandbox Code Playgroud) 我需要调用一些需要WS-Security的第三Web服务.我使用以下配置创建了一个WCF端点:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="TestBinding">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="Certificate" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="TestBehavior">
<callbackDebug includeExceptionDetailInFaults="true" />
<clientCredentials>
<clientCertificate findValue="Acme" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName" />
<serviceCertificate>
<authentication certificateValidationMode="PeerOrChainTrust" />
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="https://acme.com/webservices" binding="wsHttpBinding" bindingConfiguration="TestBinding" behaviorConfiguration="TestBehavior" contract="AcmeContract" name="AcmeEndpoint"></endpoint>
</client>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
问题是第三方服务器抛出以下异常:
收到协议'_http://schemas.xmlsoap.org/wsdl/soap12/',必需的协议'_http://schemas.xmlsoap.org/wsdl/soap/'.
据我所知,使用wsHttpBinding将导致WCF发送SOAP 1.2请求,而使用basicHttpBinding将导致SOAP 1.1请求.由于WS-Security部分是必需的,据我所知,我必须使用wsHttpBinding.我的问题是如何强制SOAP 1.1请求?我有什么选择?