【问题标题】:How can i return several fields directly in a WCF Response object, rather than wrapped in another object?如何直接在 WCF 响应对象中返回多个字段,而不是包装在另一个对象中?
【发布时间】:2020-08-24 15:48:56
【问题描述】:

我如何从 WCF 服务返回响应,其字段直接位于 Response 对象中,而不是包装在 Response 对象中的 Result 对象中?

我继承并维护了运行良好的 WCF 服务。我被要求添加一个做某事的 passthru 方法,然后将请求路由到另一个现有服务(由其他人维护),然后按原样返回结果。我要解决的部分是现有服务在其响应对象中返回松散字段,但 WCF 似乎希望我返回单个值(或包装在对象中的值)。

我该怎么做:

  • 在 methodResponse 对象中返回多个松散字段,或者
  • 拦截 methodResponse 并自己构建/编写,
  • 还有什么东西可以按原样传回结果?

例如,我想返回这个(即从其他服务返回的内容)

<soap:Body>
    <MethodResponse>
        <Value1>123</Value1>
        <Value2>abc</Value2>
        <Value3>http://123.com</Value3>
        <Value4>Success</Value4>
    </MethodResponse>
</soap:Body>

而不是这个

<soap:Body>
    <MethodResponse>
        <MethodResult>
            <Value1>123</Value1>
            <Value2>abc</Value2>
            <Value3>http://123.com</Value3>
            <Value4>Success</Value4>
        </MethodResult>
    </MethodResponse>
</soap:Body>

【问题讨论】:

  • 如果我的回复对您有帮助,您可以标记为答案。如果问题没有解决,请告诉我。
  • 叮,我无法让它工作。还有其他现有的方法我没有 [MessageContract] 属性,所以我认为这可能导致我的问题(ServiceActivationException)。所以这对我不起作用。这是一个全有或全无的情况,我必须对所有方法都做同样的事情吗?

标签: c# web-services wcf


【解决方案1】:

如果在服务操作签名中没有使用 MessageContractAttribute,WCF 将创建一个包装元素来保存 SOAP 正文中的参数。这是默认的 WCF 行为。

解决方法是在对象上使用[MessageContract]。

这是我的演示:

[MessageContract]
public class MethodResponse
{
    [MessageBodyMember]
    public string value1 { get; set; }
    [MessageBodyMember]
    public string value2 { get; set; }
    [MessageBodyMember]
    public string value3 { get; set; }
}

上面的类是服务器返回的类。

这是客户端收到的消息。

有关MessageContract的更多信息,请参考以下链接:

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-message-contracts

如果问题仍然存在,请随时告诉我。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using static ConsoleApp65.soap;

namespace ConsoleApp65
{
    [MessageContract]
    public class MethodResponse
    {
        [MessageBodyMember]
        public string value1 { get; set; }
        [MessageBodyMember]
        public string value2 { get; set; }
        [MessageBodyMember]
        public string value3 { get; set; }
    }
  
    [ServiceContract]
    public interface Test
    {
        [OperationContract]
        MethodResponse Test();
    }
    public class TestService : Test
    {
        public MethodResponse Test()
        {
            MethodResponse data = new MethodResponse();
            data.value1 = "1";
            data.value2 = "2";
            data.value3 = "3";
            return data;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Step 1: Create a URI to serve as the base address.
            Uri baseAddress = new Uri("http://localhost:8000/GettingStarted/");

            // Step 2: Create a ServiceHost instance.
            ServiceHost selfHost = new ServiceHost(typeof(TestService), baseAddress);

            try
            {
                // Step 3: Add a service endpoint.
                selfHost.AddServiceEndpoint(typeof(Test), new WSHttpBinding(), "CalculatorService");

                // Step 4: Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                // Step 5: Start the service.
                selfHost.Open();
                Console.WriteLine("The service is ready.");

                // Close the ServiceHost to stop the service.
                Console.WriteLine("Press <Enter> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();
                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }
    }
}

【讨论】:

  • 丁,谢谢,但是,嗯。一定是做错了什么。我收到 500 System.ServiceModel.ServiceActivationException。除了返回该类型的对象之外,方法本身是否需要做任何不同的事情?
  • 不需要进行其他操作,可能需要更新服务引用。
猜你喜欢
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多