WCF:如何真正使用 WSHttpBinding?我得到了异常

发布于 2024-10-21 15:10:50 字数 2765 浏览 4 评论 0原文

我创建了一个 WCF 服务:

Shared.dll:

[ServiceContract(ConfigurationName = "ICalculator")]
public interface ICalculator
{
    [OperationContract()]
    int Add(int a, int b);
}

服务器:

[ServiceBehavior()]
public class Calculator : ICalculator
{
    public int Add(int a, int b) { return a + b; }
}

客户端(尝试#1):

public class CalculatorClient : ClientBase<ICalculator>, ICalculator
{
    private static Binding binding = new WSHttpBinding("MyConfig");
    private static EndpointAddress remoteAddress = new EndpointAddress(...);

    public CalculatorClient() : base(binding, remoteAddress) { }

    public int Add(int a, int b)
    {
        return Channel.Add(a, b); //Exception
    }
}

客户端(尝试#2): -- 注意:我添加了一个服务引用,而不是自己创建一个 CalculatorClient(.NET 为我创建了它)。

static void Main(string[] args)
{
    Binding binding = new WSHttpBinding("MyConfig");
    EndpointAddress remoteAddress = new EndpointAddress(...);
    CalculatorClient client = new CalculatorClient(binding, remoteAddress);
    int result = client.Add(5, 4); //Exception
}

客户端(尝试#3): - 我将其更改为 BasicHttpBinding() 而不是

static void Main(string[] args)
{
    Binding binding = new BasicHttpBinding("MyConfig");
    EndpointAddress remoteAddress = new EndpointAddress(...);
    CalculatorClient client = new CalculatorClient(binding, remoteAddress);
    int result = client.Add(5, 4); //This works!
}

app.config:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="MyConfig" /> <!-- did not add anything to this yet -->
      </wsHttpBinding>
    </bindings>
</system.serviceModel>


The exception I get is: Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost/CalculatorService.svc. The client and service bindings may be mismatched. I don't see how they can be mismatched when I use a shared dll file between my server and client. The BasicHttpBinding works, just not the WSHttpBinding (I haven't even attempted WS2007HttpBinding.


异常:[System.ServiceModel.ProtocolException] {“内容类型 application/soap+xml;服务不支持 charset=utf-8 http://localhost/CalculatorService.svc 客户端和服务绑定可能不匹配。”} 内部异常:[System.Net.WebException] 远程服务器返回错误:(415) 无法处理消息,因为内容类型为“application/soap+xml;” charset=utf-8' 不是预期的类型 'text/xml;字符集=utf-8'..

I created a WCF Service:

Shared.dll:

[ServiceContract(ConfigurationName = "ICalculator")]
public interface ICalculator
{
    [OperationContract()]
    int Add(int a, int b);
}

Server:

[ServiceBehavior()]
public class Calculator : ICalculator
{
    public int Add(int a, int b) { return a + b; }
}

Client (Attempt #1):

public class CalculatorClient : ClientBase<ICalculator>, ICalculator
{
    private static Binding binding = new WSHttpBinding("MyConfig");
    private static EndpointAddress remoteAddress = new EndpointAddress(...);

    public CalculatorClient() : base(binding, remoteAddress) { }

    public int Add(int a, int b)
    {
        return Channel.Add(a, b); //Exception
    }
}

Client (Attempt #2): -- Note: I added a Service Reference instead of creating a CalculatorClient myself (.NET created it for me).

static void Main(string[] args)
{
    Binding binding = new WSHttpBinding("MyConfig");
    EndpointAddress remoteAddress = new EndpointAddress(...);
    CalculatorClient client = new CalculatorClient(binding, remoteAddress);
    int result = client.Add(5, 4); //Exception
}

Client (Attempt #3): -- I changed it to be a BasicHttpBinding() instead

static void Main(string[] args)
{
    Binding binding = new BasicHttpBinding("MyConfig");
    EndpointAddress remoteAddress = new EndpointAddress(...);
    CalculatorClient client = new CalculatorClient(binding, remoteAddress);
    int result = client.Add(5, 4); //This works!
}

app.config:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="MyConfig" /> <!-- did not add anything to this yet -->
      </wsHttpBinding>
    </bindings>
</system.serviceModel>


The exception I get is: Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost/CalculatorService.svc. The client and service bindings may be mismatched. I don't see how they can be mismatched when I use a shared dll file between my server and client. The BasicHttpBinding works, just not the WSHttpBinding (I haven't even attempted WS2007HttpBinding.


Exception: [System.ServiceModel.ProtocolException]
{"Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost/CalculatorService.svc. The client and service bindings may be mismatched."}
Inner Exception: [System.Net.WebException]
The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..

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

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

发布评论

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

评论(1

℡寂寞咖啡 2024-10-28 15:10:50

您需要设置要在 WSHttpBinding

http://msdn.microsoft.com/en-us/library/ms731884(v=VS.90).aspx

使用示例客户端/服务器 WSHttpBinding 进行更新,默认安全

客户端


    class Program
    {
        static void Main(string[] args)
        {
          var calcClient = new CalcClient();
          int i = 1;
          int j = 2;
          Console.WriteLine("Result of Adding {0} and {1} is {2}", i, j, calcClient.Add(i, j));
          Console.ReadKey();
        }
    }

    public class CalcClient : ICalculator
    {
        public CalcClient()
        {
            CalcProxy = ChannelFactory.CreateChannel(new WSHttpBinding(), new EndpointAddress("http://localhost:5050/CalcServer"));
        }

        ICalculator CalcProxy { get; set; }

        public int Add(int a, int b)
        {
            return CalcProxy.Add(a, b);
        }
    }

服务器


class Program
    {
        static void Main(string[] args)
        {
            var host = new ServiceHost(typeof (CalcSvr));
            host.AddServiceEndpoint(typeof (ICalculator), new WSHttpBinding(), "http://localhost:5050/CalcServer");
            host.Open();
            Console.WriteLine("Server Running");
            Console.ReadKey();
        }
    }

You need to set the security to be used on the WSHttpBinding

http://msdn.microsoft.com/en-us/library/ms731884(v=VS.90).aspx

Updated with Sample Client/Server WSHttpBinding, default security

Client


    class Program
    {
        static void Main(string[] args)
        {
          var calcClient = new CalcClient();
          int i = 1;
          int j = 2;
          Console.WriteLine("Result of Adding {0} and {1} is {2}", i, j, calcClient.Add(i, j));
          Console.ReadKey();
        }
    }

    public class CalcClient : ICalculator
    {
        public CalcClient()
        {
            CalcProxy = ChannelFactory.CreateChannel(new WSHttpBinding(), new EndpointAddress("http://localhost:5050/CalcServer"));
        }

        ICalculator CalcProxy { get; set; }

        public int Add(int a, int b)
        {
            return CalcProxy.Add(a, b);
        }
    }

Server


class Program
    {
        static void Main(string[] args)
        {
            var host = new ServiceHost(typeof (CalcSvr));
            host.AddServiceEndpoint(typeof (ICalculator), new WSHttpBinding(), "http://localhost:5050/CalcServer");
            host.Open();
            Console.WriteLine("Server Running");
            Console.ReadKey();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文