WCF在ServiceContract中的WebGet批注中为ResponseFormat属性提供了两个选项.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "greet/{value}", BodyStyle = WebMessageBodyStyle.Bare)]
string GetData(string value);
[OperationContract]
[WebGet(UriTemplate = "foo", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
string Foo();
Run Code Online (Sandbox Code Playgroud)
ResponseFormat的选项是WebMessageFormat.Json和WebMessageFormat.Xml.是否可以编写自己的网络信息格式?我希望当客户端调用foo()方法时,他会获得原始字符串 - 没有json或xml包装器.
我有一个RESTful WCF Web服务,其中包含以下API:
[WebGet(ResponseFormat = WebMessageFormat.Json)]
MyResponseContract GetFileInfo();
Run Code Online (Sandbox Code Playgroud)
尝试命中端点(使用SOAPUI)时,我看到以下错误消息:
服务器遇到处理请求的错误.请参阅服务帮助页面以构建对服务的有效请求.
我有SOAPUI设置为使用GET方法调用命中它.当我将其切换到没有正文的POST时,它会失败并显示以下消息:
方法不允许.
这很有道理:无法通过POST获得GET.所以我更新了我的代码如下:
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
MyResponseContract GetFileInfo();
Run Code Online (Sandbox Code Playgroud)
现在我使用POST方法从SOAPUI调用它并且它可以工作.好奇.所以我现在更改我的代码如下:
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "GET")]
MyResponseContract GetFileInfo();
Run Code Online (Sandbox Code Playgroud)
我在一些帖子中看到,这基本上等同于一个WebGet
属性.这也行不通.
所以我的问题是:WebGet
即使我不接受参数或使用自定义UriTemplate,为什么这不起作用?
我试图点击它(它在IIS本地托管)的Url是:
http://localhost/Utilities/API/GetFileInfo
更新 鉴于下面的评论和给出的答案,我仍然面临这个问题.一些额外的细节.
我的web层web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="10000000" />
</webHttpEndpoint>
</standardEndpoints>
<behaviors>
<endpointBehaviors>
<behavior name="exampleBehavior">
<callbackDebug includeExceptionDetailInFaults="true" />
<enableWebScript />
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors> …
Run Code Online (Sandbox Code Playgroud) 我有运营合同:
[System.ServiceModel.Web.WebGet( UriTemplate = "c" , BodyStyle = WebMessageBodyStyle.Bare )]
[OperationContract]
string Connect ( );
Run Code Online (Sandbox Code Playgroud)
我把它实现为:
public string Connect ( )
{
return "<a href='someLingk' >Some link</a>";
}
Run Code Online (Sandbox Code Playgroud)
当我去那个链接时,我得到:
如何将响应格式化为html?甚至是纯文本.我不想回复HTML或json ...
我知道我可以创建一个查询服务的网站,但我只想创建一个适用于任何浏览器的简单"类似于控制台"的应用程序......
WebGetAttribute是使用Method ="GET"的WebInvokeAttribute的语法糖吗?或者是否存在潜在的差异?
我找不到[WebInvoke]
和[WebGet]
.我已经添加了System.ServiceModel引用.问题是什么?我使用.NET Framwork 4
我有一个自托管的WCF应用程序使用基本HTTP绑定,没有SSL,在.NET Framework 4.0上的控制台应用程序中运行.
我在一个方法上有一个WebGet属性,它将一个人类可读的字符串作为"冒烟测试"返回.
如果我有一个ASP.NET webforms页面,我会使用Request.UrlReferrer或ServerVariables("HTTP_REFERER")来查看客户端是否自愿提供重定向信息.
我怎么能用WCF做到这一点?
谢谢.
我创建了一个OData端点(使用实体框架,WCF数据服务)
并添加了一个自定义测试WebGet测试方法,如下所示:
[WebGet(UriTemplate = "{text}")]
public IQueryable<string> SplitString(string text)
{
if (text == null) throw new DataServiceException("text not specified");
var result = (from s in text.Split('-') orderby s select s);
return result.AsQueryable();
}
Run Code Online (Sandbox Code Playgroud)
和配置行:
config.SetServiceOperationAccessRule("SplitString", ServiceOperationRights.All);
Run Code Online (Sandbox Code Playgroud)
但是,无论我如何指定url,我都无法填写文本参数.(它总是为空).
so:
http://localhost/myservice.svc/SplitString/testtext
不起作用(它抛出我的异常,因为param为null).用于使参数起作用的正确url格式(或UriTemplate)是什么?
我发现odata和WebGet的唯一例子只有一个没有任何参数的示例方法.
可能重复:
当使用WCF,LINQ,JSON时,无法序列化'System.Linq.Enumerable ...'类型的参数
嗨,
如果我的方法signiature看起来像这样,它工作正常.
[WebGet]
MyClass[] WebMethod()
Run Code Online (Sandbox Code Playgroud)
如果签名看起来像这样
[WebGet]
IEnumerable<T> WebMethod()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:无法序列化'XYZT + <WebMethod> d__2c'类型的参数(对于操作'WebMethod',合同'IService'),因为它不是确切的类型'System.Collections.Generic.IEnumerable`1 [XYZT] '在方法签名中并且不在已知类型集合中.要序列化参数,请使用ServiceKnownTypeAttribute将类型添加到操作的已知类型集合中.
我试过添加.ServiceKnownType(typeof运算(IEnumerable的))
同样的错误.
这是2010 beta 2中的错误,或者这可能是正确的吗?
谢谢