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

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

概述我正在尝试编写并调用WCF Web服务,下面是详细信息: Web.config文件: <add relativeAddress="FetchData.svc" service="WCF.Services.FetchData" /><service name="WCF.Services.FetchData"> <endpoint address="" binding="webHttpBi 我正在尝试编写并调用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,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.","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,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.

如果是您的合同,则违反第二个限制.最简单的解决方法是创建适当的MessageContract

[MessageContract]public class RetrIEvePublishedDatainput{  [MessageBodyMember] public string Pub;  [MessageBodyMember] public int Number;}private Message RetrIEvePublishedData(RetrIEvePublishedDatainput input){    ....}
总结

以上是内存溢出为你收集整理的尝试调用WCF Web服务4.0时获取返回类型无效错误全部内容,希望文章能够帮你解决尝试调用WCF Web服务4.0时获取返回类型无效错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1086041.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存