有谁知道VS 2010中是否有HttpClient和HttpResponseMessage类?我似乎无法让它们工作,我是否需要添加引用或使用命名空间?
这些类最初来自REST for Vs 2008的入门套件......在vs 2010中它的内置即没有用于入门套件......
但如果缺少这些类,我该如何使用它们...
我搜索了谷歌的答案,我一直在寻找的是vs 2008的示例,即REST入门套件.
任何帮助真的很感激
提前致谢
我很困惑你何时以及为什么要在REST中使用特定的动词?
我知道基本的东西,比如:
Get -> for retrieval
Post -> adding new entity
PUT -> updating
Delete -> for deleting
Run Code Online (Sandbox Code Playgroud)
这些属性是按照我上面写的操作使用的,但我不明白为什么?如果在REST中的Get方法内部我会添加一个新实体或在POST内部更新一个实体会发生什么?或者可能在DELETE里面我添加一个实体.我知道这可能是一个noob问题,但我需要了解它.这对我来说听起来很混乱.
我的WCF服务中有以下方法:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
public int GetOne(string param1, string param2)
{
return 1;
}
Run Code Online (Sandbox Code Playgroud)
我从Flex应用程序发送xml,它需要一个如下所示的对象:{ param1: "test", param2: "test2" }并将其转换为以下请求:
POST http://localhost:8012/MyService.svc/GetOne HTTP/1.1
Accept: application/xml
Accept-Language: en-US
x-flash-version: 10,1,53,64
Content-Type: application/xml
Content-Length: 52
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: localhost:8012
Connection: Keep-Alive
Pragma: no-cache
Cookie: ASP.NET_SessionId=drsynacw0ignepk4ya4pou23
<param1>something</param1><param2>something</param2>
Run Code Online (Sandbox Code Playgroud)
我收到了错误The incoming message has an unexpected message format 'Raw'. The expected message …
我的合同细节如下.我正在使用Json响应和请求格式,也使用POST方法.如何编写客户端以在c#中使用此服务.
[OperationContract()]
[WebInvoke(UriTemplate = "/RESTJson_Sample1_Sample1Add", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int RESTJson_Sample1_Sample1Add(Int32 a, Int32 b, Int32 c);
Run Code Online (Sandbox Code Playgroud) 我有一个使用.NET 4.0用C#编写的RESTful WCF Web服务,我希望使用GZip或deflate压缩响应.(我可能也需要支持请求压缩,但这还不是必需的).它将作为Windows服务部署,即自托管WCF服务,因为IIS托管不是一个选项.
到目前为止,我的搜索还很短.大多数命中要么是在IIS中打开压缩,要么是为基于SOAP的服务编写自定义消息编码器,既不适合我的场景,也不适合我的场景.
如果您有任何指示如何做到这一点,将不胜感激!
我有那个错误:
合同“IService”中的操作“GetFields”使用具有 SOAP 标头的 MessageContract。None MessageVersion 不支持 SOAP 标头。
问题是我的操作被两个端点使用:CustomEndpoint with messageVersion="Soap12WSAddressing10" 和第二个 webHttpBehavior。
我认为这个错误引发了 Rest 有 MessageVersion.None 的问题
有什么办法解决吗?
我试图实现REST WCF以探索PUT和POST动词之间的区别.我已使用该服务在某个位置上传了一个文件.
服务实现如下:
[OperationContract]
[WebInvoke(UriTemplate = "/UploadFile", Method = "POST")]
void UploadFile(Stream fileContents);
public void UploadFile(Stream fileContents)
{
byte[] buffer = new byte[32768];
MemoryStream ms = new MemoryStream();
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = fileContents.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
ms.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
using (FileStream fs = File.OpenWrite(@"C:\temp\test.txt"))
{
ms.WriteTo(fs);
}
ms.Close();
Run Code Online (Sandbox Code Playgroud)
}
客户端代码如下:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:1922 /EMPRESTService.svc/UploadFile");
request.Method = "POST";
request.ContentType = "text/plain";
byte[] fileToSend = File.ReadAllBytes(@"C:\TEMP\log.txt"); // txtFileName contains …Run Code Online (Sandbox Code Playgroud) 我在我的WCF服务中使用基本身份验证.并且还使用ASP Membership provider进行身份验证.
Web.Config: 对于REST服务:
<webHttpBinding>
<binding name="webHttpBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="200065536" maxBufferPoolSize="200065536" maxReceivedMessageSize="200065536" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="202048000" maxStringContentLength="202048000" maxArrayLength="202048000"
maxBytesPerRead="202048000" maxNameTableCharCount="202048000"/>
<security mode="Transport">
</security>
</binding>
</webHttpBinding>
Run Code Online (Sandbox Code Playgroud)
验证类型和模式:
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="MembershipProvider" membershipProviderName="CustomMemberShipProvider" />
</serviceCredentials>
Run Code Online (Sandbox Code Playgroud)
在调用任何方法之前,我的BasicAuthentication类.代码如下所示:
namespace BasicAuth.Service
{
public class BasicAuthenticationInvoker : Attribute, IOperationBehavior, IOperationInvoker
{
#region Private Fields
private IOperationInvoker _invoker;
#endregion Private Fields
#region IOperationBehavior Members
public void ApplyDispatchBehavior(OperationDescription operationDescription,
DispatchOperation dispatchOperation)
{
_invoker = dispatchOperation.Invoker;
dispatchOperation.Invoker = this;
}
public void ApplyClientBehavior(OperationDescription operationDescription, …Run Code Online (Sandbox Code Playgroud) 我在IIS中托管了WCF 4.5 Restful服务,我正在尝试使用RemoteEndpointMessageProperty来获取使用该服务的客户端的IP地址.
代码1:
private string GetClientIP()
{
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint =
prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
return ip;
}
Run Code Online (Sandbox Code Playgroud)
代码2:
private string GetClientIP()
{
string retIp = string.Empty;
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
HttpRequestMessageProperty endpointLoadBalancer =
prop[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
if (endpointLoadBalancer.Headers["X-Forwarded-For"] != null)
{
retIp = endpointLoadBalancer.Headers["X-Forwarded-For"];
}
if (string.IsNullOrEmpty(retIp))
{
RemoteEndpointMessageProperty endpoint =
prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
retIp = endpoint.Address;
}
return retIp;
}
Run Code Online (Sandbox Code Playgroud)
但是,由于WCF服务托管在负载均衡器后面的IIS中,因此我获得的IP地址始终是负载均衡器的IP.有没有办法解决这个问题,以便我可以获得客户的真正IP?
谁知道这有什么问题?我无法从我的wcf休息服务获得json响应.
jQuery的
$.ajax({
type: 'POST',
url: "http://localhost:8090/UserService/ValidateUser",
data: {username: 'newuser', password: 'pwd'},
contentType: "application/json; charset=utf-8",
success: function(msg) {
alert(msg);
},
error: function(xhr, ajaxOptions, thrownError) {
alert('error');
}
});
服务
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class UserService: IUserService
{
private readonly IUserRepository _repository;
public UserService()
{
_repository = new UserRepository();
}
public ServiceObject ValidateUser(string username, string password)
{
//implementation
}
}
[ServiceContract]
public interface IUserService
{
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
[OperationContract]
ServiceObject ValidateUser(string username, …Run Code Online (Sandbox Code Playgroud) wcf ×10
wcf-rest ×10
c# ×5
rest ×3
ajax ×2
asp.net ×2
.net-4.0 ×1
apache-flex ×1
compression ×1
http ×1
http-verbs ×1
jquery ×1
json ×1