gpt4 book ai didi

wcf - 尝试调用 WCF webservice 4.0 时获取返回类型无效错误

转载 作者:行者123 更新时间:2023-12-04 13:46:04 25 4
gpt4 key购买 nike

我正在尝试编写和调用 WCF Web 服务,以下是详细信息:

Web.config:

<add relativeAddress="FetchData.svc" service="WCF.Services.FetchData" />

<service name="WCF.Services.FetchData">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="" name="FetchData" contract="WCF.Services.FetchData" />
</service>

FetchData 类(示例代码):
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web;
using System.Xml;
using Webservices.Services;
using Data = Webservices.Data;
using System.ServiceModel.Web;
using System.IO;
using System.Net;
using System.ServiceModel.Channels;
using System.Web.UI;
using System.Text;


namespace WCF.Services
{
[ServiceContract(Namespace = "urn:WCF.Services.FetchData")]
public class FetchData
{
Data.GetConnect mConnect = new Data.GetConnect();
private Message RetrievePublishedData(String pub, int number)
{
String strOutput = String.Empty;
if (!String.IsNullOrEmpty(pub))
{
Boolean pubURLExists = mConnect.CheckPubUrlExists(pub);

if (!pubURLExists)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
return WebOperationContext.Current.CreateTextResponse(String.Format("Requested publication '{0}' is not available.", pub), MimeTypes.TextPlain, Encoding.UTF8);
}
using (StringWriter sw = new StringWriterEncoding())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
hw.RenderBeginTag(HtmlTextWriterTag.Html);
XmlNode publishedData = mConnect.GetPublishedData(pub, number);
hw.RenderEndTag();
}
return WebOperationContext.Current.CreateTextResponse(sw.ToString(),MimeTypes.TextHTML, Encoding.UTF8);
}
}
return WebOperationContext.Current.CreateTextResponse(strOutput, MimeTypes.TextHTML, Encoding.UTF8);
}
[OperationContract]
[WebGet(UriTemplate = "/published/{number}/{*pub=default}")]
public Message FetchPublished(String pub, int number)
{
return RetrievePublishedData(pub, number);
}
}
}

现在,当我尝试浏览 Web 服务时,出现以下错误:

Web 服务 URL - http://localhost:8082/FetchData.svc
错误:
无法加载操作“FetchPublished”,因为它具有 System.ServiceModel.Channels.Message 类型的参数或返回类型,或者具有 MessageContractAttribute 和其他不同类型参数的类型。使用 System.ServiceModel.Channels.Message 或具有 MessageContractAttribute 的类型时,该方法不得使用任何其他类型的参数。

编辑:
namespace WCFWebServices
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
[ServiceContract(Namespace = "urn:WCFWebServices.fetchPush")]
public class FetchData
{
[MessageContract]
public class RetrievePublishedDataInput
{
[MessageBodyMember]
public String pub;
[MessageBodyMember]
public String number;
}
private Message RetrievePublishedData(RetrievePublishedDataInput input)
{
String strOutput = String.Empty;
String pub = input.pub;
String number = input.number;
if (!String.IsNullOrEmpty(pub))
{
Boolean pubURLExists = true;

if (!pubURLExists)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
return WebOperationContext.Current.CreateTextResponse(String.Format("Requested publication '{0}' is not available.", pub), "application/plain; charset=utf-8", Encoding.UTF8);
}
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
hw.RenderBeginTag(HtmlTextWriterTag.Html);

hw.RenderEndTag();
}
return WebOperationContext.Current.CreateTextResponse(sw.ToString(), "application/html; charset=utf-8", Encoding.UTF8);
}
}
return WebOperationContext.Current.CreateTextResponse(strOutput, "application/html; charset=utf-8", Encoding.UTF8);
}
[OperationContract]
[WebGet(UriTemplate = "/publishedData/{number}/{pub=default}")]
public Message FetchPublished(RetrievePublishedDataInput input)
{
return RetrievePublishedData(input);
}
}
}

最佳答案

我相信提到的错误是不言自明的。根据 the MSDN ,使用 Message 类有其自身的限制:

You can use the Message class as an input parameter of an operation, the return value of an operation, or both. If Message is used anywhere in an operation, the following restrictions apply:

  • The operation cannot have any out or ref parameters.
  • There cannot be more than one input parameter. If the parameter is present, it must be either Message or a message contract type.
  • The return type must be either void, Message, or a message contract type.

如果是您的契约(Contract),则违反了第二个限制。最简单的解决方法是创建适当的 MessageContract :
[MessageContract]
public class RetrievePublishedDataInput
{
[MessageBodyMember] public string Pub;
[MessageBodyMember] public int Number;
}

private Message RetrievePublishedData(RetrievePublishedDataInput input)
{
....
}

关于wcf - 尝试调用 WCF webservice 4.0 时获取返回类型无效错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20762591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com