以下是我在测试应用中使用的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.IO;
namespace MyWCFServices
{
[ServiceContract]
interface IHelloWorldService
{
[OperationContract]
String GetMessage(String name);
//[OperationContract]
//[WebInvoke(Method = "PUT",UriTemplate = "File/{fileName}")]
//[WebContentType("application/octet-stream")]
// bool UploadFile(string fileName, Stream fileContents);
[OperationContract]
[WebInvoke(UriTemplate = "UploadFile/{fileName}")]
void UploadFile(string fileName, Stream fileContent);
}
}
Run Code Online (Sandbox Code Playgroud)
它为webinvoke的编译提供了错误.有什么想法吗?
我试图通过以下链接确定客户端的IP地址:http://www.danrigsby.com/blog/index.php/2008/05/21/get-the-clients-address-in-wcf/
在.Net 3.0中,没有可靠的方法来获取连接到WCF服务的客户端的地址.在.Net 3.5中引入了一个名为RemoteEndpointMessageProperty的新属性.此属性为您提供客户端连接进入服务的IP地址和端口.获取这些信息非常简单.只需通过RemoteEndpointMessageProperty.Name从当前OperationContext的IncomingMessageProperties中提取它,然后访问Address和Port属性.
> [ServiceContract] public interface IMyService {
> [OperationContract]
> string GetAddressAsString(); }
>
> public class MyService : IMyService {
> public string GetAddressAsString()
> {
> RemoteEndpointMessageProperty clientEndpoint =
> OperationContext.Current.IncomingMessageProperties[
> RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
>
> return String.Format(
> "{0}:{1}",
> clientEndpoint.Address, clientEndpoint.Port);
> } }
Run Code Online (Sandbox Code Playgroud)
注意事项:
- 此属性仅适用于http和tcp传输.在所有其他传输上,例如MSMQ和NamedPipes,此属性将不可用.
- 地址和端口由服务计算机的套接字或http.sys报告.因此,如果客户端通过VPN或其他修改地址的代理进入,则将表示该新地址而不是客户端的本地地址.这是可取和重要的,因为这是服务看到客户端的地址和端口,而不是客户端所看到的.这也意味着可能会有一些欺骗行为.客户端或服务器之间的某个客户端可能会欺骗地址.因此,除非添加一些其他自定义检查机制,否则不要使用地址或端口进行任何安全性决策.
- 如果您在服务上使用双工,那么不仅服务会为客户端填充此属性,而且客户端还将为该服务的每次调用填充此属性.
我有WebInvoke/Post和WebGet的operationContracts.当客户端请求是WebGet时,代码有效.但是当客户端请求是WebInvoke时,我将获得WCF主机IP.有解决方案吗 谢谢.
这是界面
[OperationContract]
[WebGet(UriTemplate = RestTemplate.hello_get)]
Stream hello_get();
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = RestTemplate.hello_post)]
Stream hello_post();
// Code for …
Run Code Online (Sandbox Code Playgroud) 我找不到[WebInvoke]
和[WebGet]
.我已经添加了System.ServiceModel引用.问题是什么?我使用.NET Framwork 4
我在WCF中编写了一个REST服务,我在其中创建了一个方法(PUT)来更新用户.对于这种方法,我需要传递多个身体参数
[WebInvoke(Method = "PUT", UriTemplate = "users/user",BodyStyle=WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
public bool UpdateUserAccount(User user,int friendUserID)
{
//do something
return restult;
}
Run Code Online (Sandbox Code Playgroud)
虽然如果只有一个参数,我可以传递用户类的XML实体.如下:
var myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
myRequest.Method = "PUT";
myRequest.ContentType = "application/xml";
byte[] data = Encoding.UTF8.GetBytes(postData);
myRequest.ContentLength = data.Length;
//add the data to be posted in the request stream
var requestStream = myRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
Run Code Online (Sandbox Code Playgroud)
但是如何传递另一个参数(friendUserID)值?谁能帮我?
我需要接受表单数据到基于WCF的服务.这是界面:
[OperationContract]
[WebInvoke(UriTemplate = "lead/inff",
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int Inff(Stream input);
Run Code Online (Sandbox Code Playgroud)
这是实现(示例 - 没有错误处理和其他安全措施):
public int Inff(Stream input)
{
StreamReader sr = new StreamReader(input);
string s = sr.ReadToEnd();
sr.Dispose();
NameValueCollection qs = HttpUtility.ParseQueryString(s);
Debug.WriteLine(qs["field1"]);
Debug.WriteLine(qs["field2"]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
假设WCF,除了解析传入流之外,还有更好的方法来实现吗?
我是WCF,REST等的新手.我正在尝试编写服务和客户端.我想将xml作为字符串传递给服务并获得一些响应.
我试图将正文中的xml传递给POST方法,但是当我运行我的客户端时,它只是挂起.
当我更改服务以接受参数作为uri的一部分时,它工作正常.(当我将UriTemplate从"getString"更改为"getString/{xmlString}"并传递字符串参数时).
我正在粘贴下面的代码.
[ServiceContract]
public interface IXMLService
{
[WebInvoke(Method = "POST", UriTemplate = "getString", BodyStyle=WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
string GetXml(string xmlstring);
}
Run Code Online (Sandbox Code Playgroud)
//实施代码
public class XMLService : IXMLService
{
public string GetXml(string xmlstring)
{
return "got 1";
}
}
Run Code Online (Sandbox Code Playgroud)
string xmlDoc1="<Name>";
xmlDoc1 = "<FirstName>First</FirstName>";
xmlDoc1 += "<LastName>Last</LastName>";
xmlDoc1 += "</Name>";
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(@"http://localhost:3518/XMLService/XMLService.svc/getstring");
request1.Method = "POST";
request1.ContentType = "application/xml";
byte[] bytes = Encoding.UTF8.GetBytes(xmlDoc1);
request1.GetRequestStream().Write(bytes, 0, bytes.Length);
Stream resp = ((HttpWebResponse)request1.GetResponse()).GetResponseStream();
StreamReader rdr …
Run Code Online (Sandbox Code Playgroud) 我对Web服务很新,尤其是WCF,所以请耐心等待.
我正在编写一个API,它接受一些参数,如username,apikey和一些选项,但我还需要向它发送一个字符串,该字符串可以是几千个单词,它被操作并作为流传回.将它放在查询字符串中没有意义,所以我想我会将消息体张贴到服务中.
似乎没有一种简单的方法可以做到这一点......
我的运营合同是这样的
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate="Method1?email={email}&apikey={apikey}"+
"&text={text}&quality={qual}", BodyStyle = WebMessageBodyStyle.Bare)]
Stream Method1(string email, string apikey, string text, string qual);
Run Code Online (Sandbox Code Playgroud)
这很有效.但它是我想要提取的"文本"参数并且在帖子正文中.我读过的一件事就是将一个流作为另一个参数,如下所示:
Stream Method1(string email, string apikey, string qual, Stream text);
Run Code Online (Sandbox Code Playgroud)
然后我可以读入.但是这会抛出一个错误,说如果我想要一个流参数,它必须是唯一的参数.
那么我怎样才能实现我在这里尝试做的事情,或者在查询字符串中发送几千个单词没什么大不了的?
我目前正在开发Windows服务托管的WCF服务.其中一种方法具有URI,该URI设置为接收来自支付提供商的回调.这是接口合同......
[OperationContract]
[WebInvoke(UriTemplate = "3DSecureCallback?TrxId={id}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
void ThreeDSecureCallBack(string id, Stream body);
Run Code Online (Sandbox Code Playgroud)
我遇到的这个问题是第三方提供商发布到我们的服务.我必须提供一个回调网址.因此我们可以协调付款,我们提供带有包含交易ID的查询字符串参数的URL.
在开发此服务期间,回拨已成功.(这是在添加Steam参数之前)
但是,我们现在处于需要解析发布数据的阶段.这是第二个'Stream'参数被添加到方法签名的点.
我得到的问题是我收到以下异常......
For request in operation ThreeDSecureCallBack to be a stream the operation must have a single parameter whose type is Stream.
Run Code Online (Sandbox Code Playgroud)
通过删除id参数,只有流,我们可以获取发布数据.这在实践中不起作用,因为我还需要查询字符串参数.
有人可以建议如何解决这个问题吗?我真的很茫然.
提前致谢,
大卫
我有以下WebInvoke属性:
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
Run Code Online (Sandbox Code Playgroud)
我希望根据运行时值设置UriTemplate值.有没有办法在运行时在服务实现中设置UriTemplate?