尝试调用 WCF Webservice 4.0 时出现返回类型无效错误

2023-12-12

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

网页配置:

<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);
        }
    }
}

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

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

Error: 无法加载操作“FetchPublished”,因为它具有 System.ServiceModel.Channels.Message 类型的参数或返回类型,或者具有 MessageContractAttribute 和其他不同类型参数的类型。使用 System.ServiceModel.Channels.Message 或带有 MessageContractAttribute 的类型时,该方法不得使用任何其他类型的参数。

Edit:

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, using Message类有其自身的限制:

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

  • 该操作不能有任何 out 或 ref 参数。
  • 不能有多个输入参数。如果存在该参数,则它必须是 Message 或消息契约类型.
  • 返回类型必须是 void、Message 或消息协定类型。

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

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

private Message RetrievePublishedData(RetrievePublishedDataInput input)
{
    ....
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试调用 WCF Webservice 4.0 时出现返回类型无效错误 的相关文章

随机推荐

  • Google Play - 零支持的设备

    我知道这里有类似的问题 但似乎没有一个令人满意的答案 我正在尝试发布应用程序 但无论我尝试什么 开发人员控制台都会报告支持的设备数为零 这是我的完整清单
  • 如何获取R脚本出错时的行号?

    如果我从命令行运行一个很长的 R 脚本 R slave script R 那么我怎样才能让它在错误时给出行号呢 如果可能的话 我不想将调试命令添加到脚本中 我只是希望 R 表现得像大多数其他脚本语言一样 这不会给您行号 但它会告诉您调用堆栈
  • WPF 与 Windows 窗体

    我对 WPF 和 Windows 窗体非常困惑 WPF 相对于 Windows 窗体的用途是什么 WPF有什么用 WPF 是一个用于开发 Windows 和浏览器 应用程序的新平台 WPF不一定有replaceWindows 窗体 使用 W
  • Dojo 中的 DataGrid,包含来自 servlet 的 json 数据

    我第一次使用 JSON 并想用我的 JSON 数据填充我的数据网格 这是我的 JSON 数据 head vars s fname lname results bindings s type uri value http tn gov in
  • 按键对散列进行分组并对值求和

    我有一个哈希数组 Vegetable gt 10 Vegetable gt 5 Dry Goods gt 3 gt Dry Goods gt 2 我需要使用inject我想 但我真的一直在挣扎 我想要一个新的哈希值来反映前一个哈希值的重复键
  • 如何使用 PHP 解码以“\u”开头的内容

    如何使用 PHP 解码以 u 开头的内容 e g u4f60 u5df2 u7ecf u6dfb u52a0 u4e86 u6b64 u8bdd u9898 谢谢 对于 PHP 5 4 intl s u4f60 u5df2 u7ecf u6
  • 如何使用 PHP DOMDocument::saveHTML() 阻止 html 实体?

    由于自定义存储需求 为什么 在这里并不重要 谢谢 我必须保存 html a 特定格式的链接 例如 myDOMNode gt setAttribute href 123456 一切正常 直到我打电话saveHTML 在包含的 DOMDocum
  • AJAX POST 到 PHP(无需 JQuery)

    我有一个 PHP 作业 我决定尝试添加 AJAX 因为在我们的课堂上我们不会只学习 AJAX 而只会学习 PHP 我似乎无法得到工作的回应 然而 在 Fire Fox 控制台的网络部分中 我可以找到使用我在表单中输入的值发送的 POST 以
  • 多标签分类的特征选择 (scikit-learn)

    我正在尝试在 scikit learn 中通过卡方方法进行特征选择 sklearn feature selection SelectKBest 当我尝试将此应用于多标签问题时 我收到此警告 UserWarning Duplicate sco
  • 如何使用slf4j框架实现敏感数据的屏蔽?

    我想使用 slf4j 框架屏蔽敏感数据 例如用户名 密码 感谢您立即提供帮助 提前致谢 试试这个 1 首先 我们应该创建一个类来处理我们的日志 每行 public class PatternMaskingLayout extends Pat
  • 如何通过 JavaScript 将条目插入浏览历史记录

    如何在浏览历史记录中插入条目 以便后退按钮第一次单击时转到不同页面 第二次单击时转到原始页面 因此 如果您需要对我想要做什么进行详细解释 请访问 https secure exitjunction com howitworks jsp 我只
  • 在 Fortran 中存储具有多维索引的变量

    Question 考虑以下代码 program example implicit none integer parameter n coeffs 1000 integer parameter n indices 5 integer i re
  • c++ 执行时间比 python 慢

    我改用c 因为我听说它比python快400倍 但是当我制作一个无限循环来递增变量并打印其值时 python似乎更快 怎么可能呢 以及如何优化呢 Python脚本 x 1 while 1 print x x 1 C 代码 int x 1 w
  • 迭代器与 for 循环以及为什么像 for 循环一样引入迭代器? [复制]

    这个问题在这里已经有答案了 可能的重复 Java中增强的for循环和迭代器有什么优点 for 循环和 for each 循环之间有性能差异吗 下面的代码显示 使用 for 循环和迭代器 我们都可以迭代集合的元素 那么 for 循环和迭代器之
  • 获取字典/对象键作为打字稿中的元组

    我想从 TS 3 1 中的对象中获取具有正确类型文字的正确元组类型 interface Person name string age number ExpectType name age type ObjectKeysTuple ToTup
  • Angular 4 - 如何检查 observable 是否包含字符串

    我正在尝试确定可观察对象是否包含字符串 这个问题有两个方面 如何将结果映射回对象 然后在获得该对象后确定它是否包含特定的 orderId 我用这个例子没有运气 如何检查 RxJS Observable 是否包含 Angular2 中的字符串
  • Seaborn groupby 熊猫系列

    我想将我的数据可视化为箱线图 这些箱线图由我可怕的绘图中显示的另一个变量分组 所以我所做的就是使用 pandas 系列变量告诉 pandas 我已经对变量进行了分组 所以这就是我所做的 import pandas as pd import
  • 如何使用 Selenium 和 Python 更改 DOM 中的属性值

    我正在尝试使用 Python 和 Selenium 浏览网站 但遇到了一个我不知道如何解决的问题 假设它甚至是可能的
  • 如何在cloudfront之间设置SSL作为具有EC2自定义源的反向代理缓存?

    我的域名指向 cloudfront 而 cloudfront 又使用自定义源引用我的 EC2 实例 在本例中 它是来自 EC2 的公共 DNS 名称 例如 xxxxx us west 2 compute amazonaws com 这使得它
  • 尝试调用 WCF Webservice 4.0 时出现返回类型无效错误

    我正在尝试编写和调用 WCF Web 服务 以下是详细信息 网页配置