![](/img/trans.png)
[英]How can I return two fields using Linq on an object with more than two fields?
[英]How can i return several fields directly in a WCF Response object, rather than wrapped in another object?
如何直接從 WCF 服務返回響應,其中字段直接位於 Response 對象中,而不是包裝在 Response 對象中的 Result 對象中?
我繼承並維護了運行良好的 WCF 服務。 我被要求添加一個 passthru 方法來做某事,然后將請求路由到另一個現有服務(由其他人維護),然后按原樣返回結果。 我試圖解決的部分是現有服務在其響應對象中返回松散字段,但 WCF 似乎希望我返回單個值(或包含在對象中的值)。
我怎么可能:
例如,我想返回這個(即從其他服務返回的內容)
<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>
如果在服務操作簽名中未使用 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();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.