简体   繁体   English

如何直接在 WCF Response 对象中返回多个字段,而不是包装在另一个对象中?

[英]How can i return several fields directly in a WCF Response object, rather than wrapped in another object?

How can i return a response from a WCF service with fields directly in the Response object, rather than wrapped in a Result object within the Response object?如何直接从 WCF 服务返回响应,其中字段直接位于 Response 对象中,而不是包装在 Response 对象中的 Result 对象中?

I inherited and maintain WCF service that is working well.我继承并维护了运行良好的 WCF 服务。 I've been asked to add a passthru method that does something, then routes a request to another existing service (maintained by someone else), then returns the result as-is.我被要求添加一个 passthru 方法来做某事,然后将请求路由到另一个现有服务(由其他人维护),然后按原样返回结果。 The part i'm trying to solve is that the existing service returns loose fields within their response object, but WCF appears to want me to return a single value (or values wrapped in an object).我试图解决的部分是现有服务在其响应对象中返回松散字段,但 WCF 似乎希望我返回单个值(或包含在对象中的值)。

How might i either:我怎么可能:

  • return multiple loose fields in the methodResponse object, or在 methodResponse 对象中返回多个松散字段,或
  • intercept the methodResponse and build/write it myself,拦截methodResponse并自己构建/编写,
  • something else to pass back the result as is?还有什么可以按原样传回结果?

Example, i want to return this (ie what's returned from the other service)例如,我想返回这个(即从其他服务返回的内容)

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

instead of this而不是这个

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

If the MessageContractAttribute is not used in the service operation signature, WCF will create a wrapper element to save the parameters in the SOAP body.如果在服务操作签名中未使用 MessageContractAttribute,WCF 将创建一个包装器元素以将参数保存在 SOAP 正文中。 This is the default WCF behavior.这是默认的 WCF 行为。

The solution is to use [MessageContract] on the object.解决方案是在对象上使用 [MessageContract]。

Here is my demo:这是我的演示:

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

The above class is the class returned by the server.上面的类是服务器返回的类。

在此处输入图片说明

This is the message received by the client.这是客户端收到的消息。

For more information about MessageContract,Please refer to the following link:关于 MessageContract 的更多信息,请参考以下链接:

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

Feel free to let me know if the problem persists.如果问题仍然存在,请随时告诉我。

This is my code:这是我的代码:

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();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在具有两个以上字段的对象上使用Linq返回两个字段? - How can I return two fields using Linq on an object with more than two fields? 多态-使用从接口继承而不是直接从接口继承的对象 - Polymorphism - Using an object that inherits from an interface rather than the interface directly 如何返回流而不是写入磁盘? - How can I return a stream rather than writing to disk? 如何检查表示数字的对象是否大于另一个对象? - How can I check if an object representing a number is greater than another? 如何返回列表 <object> 在WCF中 - How to return a List<object> in WCF 如何从任务异步返回WCF响应中的流? - How can I return a stream in a WCF response asynchronously from a task? 我如何在WCF Web服务中返回动态json / xml对象 - How can i return a dynamic json/xml object in my WCF webservice 如何从一个对象(另一个对象的副本)中删除多个项目,而又不影响其中一个派生对象? - How can I delete several items from an object, which is copy of another object, without affecting the one of which is derived? 如何在代码而不是配置中创建WCF EndPointBehaviors? - How do I create WCF EndPointBehaviors in Code rather than the configuration? 如何返回具有动态字段的对象 - How to return object with dynamic fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM