尝试调用WCF Web服务4.0时获取返回类型无效错误

时间:2013-12-24 14:29:53

标签: wcf web-services c#-4.0

我正在尝试编写并调用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);
        }
    }
}

现在,当我尝试浏览网络服务时,我收到以下错误:

网络服务网址 - http://localhost:8082/FetchData.svc

错误: 操作&#39; FetchPublished&#39;无法加载,因为它具有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);
            }
        }      
}

1 个答案:

答案 0 :(得分:11)

我相信提到的错误是非常自我解释的。根据{{​​3}},使用Message类有自己的限制:

  

您可以将Message类用作操作的输入参数,操作的返回值或两者。如果在操作中的任何位置使用Message,则适用以下限制:

     
      
  • 操作不能有任何out或ref参数。
  •   
  • 输入参数不能超过一个。 如果参数存在,则必须是“消息”或消息合同类型
  •   
  • 返回类型必须为void,Message或消息合同类型。
  •   

如果是您的合同,则违反第二项限制。最简单的解决方法是创建正确的the MSDN

[MessageContract]
public class RetrievePublishedDataInput
{
  [MessageBodyMember] public string Pub;
  [MessageBodyMember] public int Number;
}

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