【问题标题】:How to get wcf response in XML format which is referenced through service reference in class library project如何获取通过类库项目中的服务引用引用的 XML 格式的 wcf 响应
【发布时间】:2020-03-23 07:00:47
【问题描述】:
// Created obj for wcf service
ServiceSummary.ImageService.ManagerServiceClient obj1 = new ServiceSummary.ImageService.ManagerServiceClient(); 

// Forming a request body
var request = new ImageService.GetImageRequest
                {
                    UserContextData = new ImageService.UserContextData
                    {
                        Country = Country.ToUpper(),
                        Region = Region.ToUpper()
                    },
                };

// Invoking GetImageResponse and storing result in response variable
var response = obj1.GetImageResponse(request);

返回的是class类型的响应——如何获取XML格式的响应?

【问题讨论】:

标签: c# asp.net wcf class-library


【解决方案1】:

我有点困惑为什么我们需要原始 XML 数据。但是我们完全可以通过IClientMessageInspector获得源消息,SOAP消息包络。
https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.iclientmessageinspector?redirectedfrom=MSDN&view=netframework-4.8
这是一个示例,假设您使用客户端代理调用服务。

public class ClientMessageLogger : IClientMessageInspector
    {
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            Console.WriteLine(reply);
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            return null;
        }
    }
    public class CustContractBehaviorAttribute : Attribute, IContractBehavior, IContractBehaviorAttribute
    {
        public Type TargetContract => typeof(IService);

        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            return;
        }

        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.ClientMessageInspectors.Add(new ClientMessageLogger());
        }

        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
            return;
        }

        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
        {
            return;
        }
}

然后将契约行为应用到自动生成的服务契约上。

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IService")]
    [CustContractBehavior]
public interface IService {

结果。

如果有什么可以帮助的,请随时告诉我。

【讨论】:

  • 您好,感谢您的回复,上面会为您获取带有所有标签的 xml,我只需要 xml 格式的它,所以它采用了打击格式:var response = obj1.GetImageResponse(request);使用 (var stream = new MemoryStream()) { var serialOg = new XmlSerializer(typeof(ImageService.GetImageResponse()); var xtww = new XmlTextWriter(stream, new UTF8Encoding(false)); serialOgofCartsvc.Serialize(xtww, response); xtww.Close(); }
  • 可以,可以再次序列化结果,返回XML格式的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
相关资源
最近更新 更多