0

我在我的软件项目中使用 WCF 接口包装现有的 ASMX Web 服务作为过渡阶段,以节省时间。除了一个返回 System.String 的函数外,这很好用。

原始 ASMX 服务根据给定的参数返回文本或 XML。这在 ASMX 中不是问题。在 WCF 中,返回的值(如果是 XML)被转义为:&lt;gml&gt;应该在哪里<gml>。请参阅下面的 SOAP。

要求

POST http://someuri.org/WebServices/Utils.svc HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: http://www.someuri.org/IUtils/Function
Content-Length: 283
Accept: */*
User-Agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)
Host: foo.bar.org
Connection: Keep-Alive

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <Function xmlns="http://www.someri.org/">
            <type>...</type>
            <input1>...</input1>
            <input2>...</input2>
            <input3>true</input3>
        </Function >
    </s:Body>
</s:Envelope>

回复

HTTP/1.1 200 OK
Date: Fri, 04 May 2012 11:40:01 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 2070

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <FunctionResponse xmlns="http://www.crotec.nl/">   
            <FunctionResult>&lt;gml&gt;data&lt;/gml&gt;</FunctionResult>
        </FunctionResponse>
    </s:Body>
</s:Envelope>

一些谷歌搜索让我返回一个 System.IO.Stream 对象。

string result = DoStuff(arg1, arg2, arg3);            
byte[] bin = Encoding.UTF8.GetBytes(result);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return new System.IO.MemoryStream(bin);

这在一定程度上有效。

string result =  "02010415DBD800D7E17577787A626978";
byte[] bin = {48,50,48,49,48,52,49,53,68,66,68,56,48,48,68,55,69,49,55,53,55,55,55,56,55,65,54,50,54,57,55,56};

然而,SOAP 消息中的返回结果是:

MDIwMTA0MTVEQkQ4MDBEN0UxNzU3Nzc4N0E2MjY5Nzg=

所以结果输出是乱码(我认为,再次,由消息编码(?)引起)

该方法是带有 OperationContract 的 attr,并且该服务托管在 IIS6 中,具有以下 ABC:

<service name="WebServices.BeheerUtils" behaviorConfiguration="Services.ServiceBehavior">
    <!-- Service Endpoints -->
    <endpoint address="" binding="basicHttpBinding" contract="WebServices.IUtils"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>

任何想法为什么输出是乱码或如何防止 HTML 编码?

界面

[OperationContract]
System.IO.Stream Function(string type, string input1, string input2, string input3);

执行

public new System.IO.Stream Function(string type, string input1, string input2, string input3)
{
    // Call the old ASMX method
    string result = DoStuff(type, input1, input2, input3, true);

    byte[] bin = Encoding.UTF8.GetBytes(result);
    WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
    return new System.IO.MemoryStream(bin);
}
4

4 回答 4

1

我不能同意这种说法:

这在 ASMX 中不是问题。在 WCF 中,返回的值(如果是 XML)被转义为:&lt;gml&gt;应该在哪里<gml>

我用方法创建了一个服务:

[OperationContract]
string GetXml(string str);

public string GetXml(string str)
{
    return "<gml>" + str + "</gml>";
}

然后我使用生成的 WCF 客户端调用服务:

var client = new MyWcfServiceClient();
var result = client.GetXml("test");

结果是:

<gml>test</gml>

更新

只是为了确保您这是 xml 的标准行为方式,请执行以下测试并检查 的值respone.FunctionResult

public class FunctionResponse
{
    public string FunctionResult { get; set; }
}

public void Test()
{
    var serialized = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<FunctionResponse>   
    <FunctionResult>&lt;gml&gt;data&lt;/gml&gt;</FunctionResult>
</FunctionResponse>";
    var ser = new XmlSerializer(typeof(FunctionResponse));
    using (var stringReader = new StringReader(serialized))
    {
        using (var xmlReader = new XmlTextReader(stringReader))
        {
            var response = ser.Deserialize(xmlReader);                    
        }
    }            
}
于 2012-05-04T11:37:55.700 回答
1

如果你的函数返回一个字符串,那么它应该被编码是正常的。

您可以尝试声明您的函数返回一个XmlNode而不是一个字符串。

于 2012-05-04T12:01:37.240 回答
0

使用 basicHttpBinding 而不是 wsHttpBinding。

于 2012-05-04T10:57:55.420 回答
0

所以我摆弄了一些返回类型,这就是我能做的:

string result = DoStuff(type, input1, input2, input3, true);

XDocument d = new XDocument();
XDocument basis = new XDocument(new XElement("result"));

// Load the result into a XDocument, add the result as a value of the wrapper element if the format is invalid    
try
{
    d = XDocument.Parse(result);
    basis.Root.Add(d.Root);
}
catch (Exception)
{
    basis.Root.Value = result;
}    

// Return the XElement
return basis.Root;

我基本上将所有响应包装在一个新的 Root 元素中。这给了我封装的 SOAP 中的文字 XML,而不是“html 编码”。尽管它可以满足我的需求,但我觉得很尴尬。但是我发现没有其他解决方案适合我的需要,因此我将其作为最终解决方案提出。

于 2012-05-07T07:05:37.393 回答