内容类型文本/xml;服务不支持 charset=utf-8

发布于 2024-12-17 12:25:48 字数 679 浏览 2 评论 0原文

我的 WCF 服务有问题。 我有一个控制台应用程序,我需要在不使用 app.config 的情况下使用该服务,因此我必须通过代码设置端点等。 我确实有对 svc 的服务引用,但我无法使用 app.config。 这是我的代码:

BasicHttpBinding binding = new BasicHttpBinding();

EndpointAddress address = new EndpointAddress("http://localhost:8731/WcfServicio/MiServicio");

MiServicioClient svc = new MiServicioClient(binding, address);
object ob = svc.PaisesObtener();

在最后一行,当我执行 svc.PaisesObtener() 时,出现错误:

Content Type text/xml; charset=utf-8 was not supported by service
http://localhost:8731/WcfServicio/MiServicio.  The client and service bindings may be mismatched.

I have a problem with a WCF service.
I have a console application and I need to consume the service without using app.config, so I had to set the endpoint, etc. by code.
I do have a service reference to the svc, but I can't use the app.config.
Here's my code:

BasicHttpBinding binding = new BasicHttpBinding();

EndpointAddress address = new EndpointAddress("http://localhost:8731/WcfServicio/MiServicio");

MiServicioClient svc = new MiServicioClient(binding, address);
object ob = svc.PaisesObtener();

At the last line when I do svc.PaisesObtener() I get the error:

Content Type text/xml; charset=utf-8 was not supported by service
http://localhost:8731/WcfServicio/MiServicio.  The client and service bindings may be mismatched.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

独自唱情﹋歌 2024-12-24 12:25:49

时,我看到了这种行为

   <service name="A.B.C.D" behaviorConfiguration="returnFaults">
        <endpoint contract="A.B.C.ID" binding="basicHttpBinding" address=""/>
    </service>

今天,当web.config 中缺少 。 service.svc 文件已存在并已得到服务。花了一段时间才意识到问题不在于绑定配置本身......

I've seen this behavior today when the

   <service name="A.B.C.D" behaviorConfiguration="returnFaults">
        <endpoint contract="A.B.C.ID" binding="basicHttpBinding" address=""/>
    </service>

was missing from the web.config. The service.svc file was there and got served. It took a while to realize that the problem was not in the binding configuration it self...

眼趣 2024-12-24 12:25:49

我今天在尝试创建WCF服务代理时看到了这个问题,都使用VS2010和svcutil。

我所做的一切都是使用 basicHttpBinding (因此 wsHttpBinding 没有问题)。

在我的记忆中,MSDN 第一次实际上为我提供了解决方案,链接如下 如何:使用配置文件发布服务的元数据。我需要更改的行位于我的服务 app.config 文件内的 MEX 服务行为元素内的行为元素内。我将其从 改为

<serviceMetadata httpGetEnabled="true"/>  

<serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>

就像魔术一样,错误消失了,我能够创建服务代理。请注意,有一个相应的 MSDN 条目用于使用代码而不是配置文件: 如何:使用代码发布服务的元数据。

(当然,Policy15 - 我怎么可能忽略了这

一点 ???) “问题”:我的服务需要公开 3 个不同的端点,每个端点支持不同的合约。对于我需要构建的每个代理,我必须注释掉其他 2 个端点,否则 svcutil 会抱怨它无法解析基本 URL 地址。

I saw this problem today when trying to create a WCF service proxy, both using VS2010 and svcutil.

Everything I'm doing is with basicHttpBinding (so no issue with wsHttpBinding).

For the first time in my recollection MSDN actually provided me with the solution, at the following link How to: Publish Metadata for a Service Using a Configuration File. The line I needed to change was inside the behavior element inside the MEX service behavior element inside my service app.config file. I changed it from

<serviceMetadata httpGetEnabled="true"/>  

to

<serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>

and like magic the error went away and I was able to create the service proxy. Note that there is a corresponding MSDN entry for using code instead of a config file: How to: Publish Metadata for a Service Using Code.

(Of course, Policy15 - how could I possibly have overlooked that???)

One more "gotcha": my service needs to expose 3 different endpoints, each supporting a different contract. For each proxy that I needed to build, I had to comment out the other 2 endpoints, otherwise svcutil would complain that it could not resolve the base URL address.

十六岁半 2024-12-24 12:25:49

我在使用通道工厂时遇到了类似的问题。这实际上是由于端点中指定的合同错误造成的。

I was facing the similar issue when using the Channel Factory. it was actually due to wrong Contract specified in the endpoint.

烟若柳尘 2024-12-24 12:25:49

对于通过搜索到达这里的任何人:

内容类型'application/json; charset=utf-8' 不是预期的类型 'text/xml;字符集=utf-8

或该错误的某些子集:

在我的例子中,由于构建和运行没有正确属性的服务,导致了类似的错误。当我尝试更新客户端应用程序中的服务引用时,收到此错误消息。当我将 [DataContract][DataMember] 属性正确应用到我的自定义类时,问题得到了解决。

如果您的服务已设置并正常运行,但在您对其进行编辑后出现故障,则这很可能适用。

For anyone who lands here by searching:

content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8

or some subset of that error:

A similar error was caused in my case by building and running a service without proper attributes. I got this error message when I tried to update the service reference in my client application. It was resolved when I correctly applied [DataContract] and [DataMember] attributes to my custom classes.

This would most likely be applicable if your service was set up and working and then it broke after you edited it.

眼泪都笑了 2024-12-24 12:25:49

我在 .net 6.0 中遇到了这个问题

,问题是 Soap 版本,BasicHttpBinding 默认目标为 Soap 1.1,但该服务使用 Soap 1.2。

解决方案是创建一个针对 Soap 1.2 的自定义绑定:

private Binding GetBindingConfiguration()
{
    var textBindingElement = new TextMessageEncodingBindingElement()
    {
        MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
    };

    var httpsBindingElement = new HttpsTransportBindingElement()
    {
        MaxReceivedMessageSize = int.MaxValue,
        RequireClientCertificate = true //my service require certificate
    };

    return new CustomBinding(textBindingElement, httpsBindingElement);
}

var binding = GetBindingConfiguration();
var address = new EndpointAddress("https://nfe.sefa.pr.gov.br/nfe/NFeAutorizacao4"); //Brazil NF-e endpoint that I had to consume.

var svc = new MyService(binding, address);

//my service requires certificate
svc.ClientCredentials.ClientCertificate.Certificate = certificado; 

object ob = svc.PaisesObtener(); //call the method

I had this problem in .net 6.0

The problem was the Soap Version, the BasicHttpBinding targets Soap 1.1 by default, but the service uses Soap 1.2.

The solution was to create a custom binding that targets Soap 1.2:

private Binding GetBindingConfiguration()
{
    var textBindingElement = new TextMessageEncodingBindingElement()
    {
        MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
    };

    var httpsBindingElement = new HttpsTransportBindingElement()
    {
        MaxReceivedMessageSize = int.MaxValue,
        RequireClientCertificate = true //my service require certificate
    };

    return new CustomBinding(textBindingElement, httpsBindingElement);
}

var binding = GetBindingConfiguration();
var address = new EndpointAddress("https://nfe.sefa.pr.gov.br/nfe/NFeAutorizacao4"); //Brazil NF-e endpoint that I had to consume.

var svc = new MyService(binding, address);

//my service requires certificate
svc.ClientCredentials.ClientCertificate.Certificate = certificado; 

object ob = svc.PaisesObtener(); //call the method
不羁少年 2024-12-24 12:25:49

我最近也面临同样的问题。经过几个小时的努力,终于通过添加找到了一个解决方案

Factory="System.ServiceModel.Activation.WebServiceHostFactory"
to your SVC markup file. e.g.
ServiceHost Language="C#" Debug="true" Service="QuiznetOnline.Web.UI.WebServices.LogService" 
Factory="System.ServiceModel.Activation.WebServiceHostFactory" 

,现在您可以编译并运行它了。成功运行您的应用程序。

I was also facing the same problem recently. after struggling a couple of hours,finally a solution came out by addition to

Factory="System.ServiceModel.Activation.WebServiceHostFactory"
to your SVC markup file. e.g.
ServiceHost Language="C#" Debug="true" Service="QuiznetOnline.Web.UI.WebServices.LogService" 
Factory="System.ServiceModel.Activation.WebServiceHostFactory" 

and now you can compile & run your application successfully.

梦旅人picnic 2024-12-24 12:25:49

我再次强调,必须在 web.config 文件中正确指定命名空间、svc 名称和合约:

 <service name="NAMESPACE.SvcFileName">
    <endpoint contract="NAMESPACE.IContractName" />
  </service>

示例:

<service name="MyNameSpace.FileService">
<endpoint contract="MyNameSpace.IFileService" />
</service>

(这些示例中省略了不相关的标签)

Again, I stress that namespace, svc name and contract must be correctly specified in web.config file:

 <service name="NAMESPACE.SvcFileName">
    <endpoint contract="NAMESPACE.IContractName" />
  </service>

Example:

<service name="MyNameSpace.FileService">
<endpoint contract="MyNameSpace.IFileService" />
</service>

(Unrelevant tags ommited in these samples)

浅浅 2024-12-24 12:25:49

就我而言,我必须在客户端应用程序的 app.config 中将 messageEncoding 指定为 Mtom,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="IntegrationServiceSoap" messageEncoding="Mtom"/>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:29495/IntegrationService.asmx"
                binding="basicHttpBinding" bindingConfiguration="IntegrationServiceSoap"
                contract="IntegrationService.IntegrationServiceSoap" name="IntegrationServiceSoap" />
        </client>
    </system.serviceModel>
</configuration>

我的客户端和服务器都使用 basicHttpBinding 。
我希望这对其他人有帮助:)

In my case, I had to specify messageEncoding to Mtom in app.config of the client application like that:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="IntegrationServiceSoap" messageEncoding="Mtom"/>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:29495/IntegrationService.asmx"
                binding="basicHttpBinding" bindingConfiguration="IntegrationServiceSoap"
                contract="IntegrationService.IntegrationServiceSoap" name="IntegrationServiceSoap" />
        </client>
    </system.serviceModel>
</configuration>

Both my client and server use basicHttpBinding.
I hope this helps the others :)

念﹏祤嫣 2024-12-24 12:25:49

我遇到了此错误,并且上述所有配置都是正确的,但我仍然收到“客户端和服务绑定可能不匹配”错误。

解决我的错误的方法是匹配以下服务节点和客户端配置文件中的 messageEncoding 属性值。它们在我的中是不同的,服务是Text,客户​​端是Mtom。将服务更改为 Mtom 以匹配客户的服务,解决了问题。

<configuration>
  <system.serviceModel>
      <bindings>
           <basicHttpBinding>
              <binding name="BasicHttpBinding_IMySevice" ... messageEncoding="Mtom">
              ...
              </binding>
           </basicHttpBinding>
      </bindings>
  </system.serviceModel>
</configuration>

I had this error and all the configurations mentioned above were correct however I was still getting "The client and service bindings may be mismatched" error.

What resolved my error, was matching the messageEncoding attribute values in the following node of service and client config files. They were different in mine, service was Text and client Mtom. Changing service to Mtom to match client's, resolved the issue.

<configuration>
  <system.serviceModel>
      <bindings>
           <basicHttpBinding>
              <binding name="BasicHttpBinding_IMySevice" ... messageEncoding="Mtom">
              ...
              </binding>
           </basicHttpBinding>
      </bindings>
  </system.serviceModel>
</configuration>
夜夜流光相皎洁 2024-12-24 12:25:48

第一个谷歌点击说:

这通常是客户端/服务器绑定中的不匹配,其中服务中的消息版本使用 SOAP 1.2(需要 application/soap+xml),而客户端中的版本使用 SOAP 1.1(发送 text/xml) 。 WSHttpBinding 使用 SOAP 1.2,BasicHttpBinding 使用 SOAP 1.1。

它通常一侧是 wsHttpBinding,另一侧是 basicHttpBinding。

First Google hit says:

this is usually a mismatch in the client/server bindings, where the message version in the service uses SOAP 1.2 (which expects application/soap+xml) and the version in the client uses SOAP 1.1 (which sends text/xml). WSHttpBinding uses SOAP 1.2, BasicHttpBinding uses SOAP 1.1.

It usually seems to be a wsHttpBinding on one side and a basicHttpBinding on the other.

美人如玉 2024-12-24 12:25:48

不要忘记检查与绑定相关的代码。
因此,如果您写道:

BasicHttpBinding binding = new BasicHttpBinding();

请确保您的所有 app.config 文件

<endpoint address="..."
          binding="basicHttpBinding" ...

<endpoint address="..."
          binding="wsHttpBinding" ...

包含以下内容。

Do not forget check the bindings-related code too.
So if you wrote:

BasicHttpBinding binding = new BasicHttpBinding();

Be sure that all your app.config files contains

<endpoint address="..."
          binding="basicHttpBinding" ...

not the

<endpoint address="..."
          binding="wsHttpBinding" ...

or so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文