我有一个WCF服务。该服务是这样的,它返回结果作为字符串。当我运行该应用程序时,我收到此错误消息。在WCF服务中使用Message类作为返回类型时出错
“的操作是getTemplate”不能被加载,因为它具有类型System.ServiceModel.Channels.Message或具有MessageContractAttribute和不同类型的其他参数的类型的一个参数或返回类型。当使用System.ServiceModel.Channels.Message或具有MessageContractAttribute的类型时,该方法不得使用任何其他类型的参数。
我相应的代码是这样的: 接口: -
[OperationContract]
[WebGet(
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetTemplate/templateid={templateID}"
)]
Message GetTemplate(string templateID);
实现: -
public Message GetTemplate(string templateID)
{
string jsonText = TemplateManager.GetJSONTemplate(templateID);
return WebOperationContext.Current.CreateTextResponse(jsonText,
"application/json; charset=utf-8", Encoding.UTF8);
}
当我用Google搜索T I才知道,我们不能使用任何可序列化的对象作为参数或使用消息时的返回类型。我想接受templateid作为参数,以获得相应的模板。有没有办法接受一个参数而不会有这个错误?
在此先感谢。
VIPIN梅农
谢谢Sixto Saez。我修正了它..我将消息的返回类型更改为流。现在它的工作正常。我使用Message作为返回类型,以获得干净的Jason输出。当作为字符串返回时,结果具有转义字符。使用无参数时,消息工作正常。 – 2012-03-13 04:39:58
是的,如果你不需要通过头文件等进入WCF管道,Stream是要走的路。 – 2012-03-13 12:38:48