消费wcf休息服务 [英] consuming wcf Rest service

查看:100
本文介绍了消费wcf休息服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



公共接口IHealthPortalLoginService
{

[OperationContract]
[WebInvoke(UriTemplate ="/loging",Method ="POST",RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
LoginDataContract日志记录(LoginDataContract objlocal);

}
[DataContract]
公共类LoginDataContract
{
#region字段
[DataMember]

public int LoginId {get;放; }

[DataMember]

公共字符串EmailAddress {get;放; }


[DataMember]

公共字符串密码{get;放; }

[DataMember]

public int DoctorId {放; }

[DataMember]

公用字符串Role {get;放; }


[DataMember]

公共布尔IsActive {放; }

[DataMember]

公共DateTime CreatedDate {get;放; }

[DataMember]
公共字符串FavoriteQuestion {get;放; }

[DataMember]
公共字符串答案{get;放; }

[DataMember]
public int状态{放; }

[DataMember]
public int LabId {get;放; }

[DataMember]
public int FrontdeskId {获取;放; }

#endregion
}


Web.config


< pre lang ="xml">& lt;?xml version =& quot; 1.0& quot;?& gt;
& lt;配置& gt;
& lt; appSettings& gt;
& lt;添加密钥=&"DBProviderType&"值=&"1"/& gt;
密码= PGI123;池= true&"/& gt;
& lt;/appSettings& gt;
& lt; system.web& gt;
& lt; httpRuntime maxRequestLength =& quot; 10000000& quot;/& gt;
& lt; compilation debug =& quot; true& quot;/& gt;
& lt//system.web& gt;
& lt;!-部署服务库项目时,必须将配置文件的内容添加到主机的
app.config文件. System.Configuration不支持库的配置文件. -& gt;
& lt; system.serviceModel& gt;
& lt; serviceHostingEnvironment multipleSiteBindingsEnabled =&"true&" /& gt;
& lt;服务& gt;
& lt;服务名称=& quot; WcfContact.Service1& quot; behaviorConfiguration =&"httpBehaviour&"& gt;
& lt;端点地址=& quot;& quot; binding =&"webHttpBinding&" contract =& quot; WcfContact.IService1& quot; behaviorConfiguration =&"httpEndpointBehaviour&" & gt;
& lt; identity& gt;
& lt; dns value =&本地主机& quot;/& gt;
& lt//identity& gt;
& lt//endpoint& gt;
<端点地址=&"mex&" binding =& quot; mexHttpBinding& quot; contract =&"IMetadataExchange&"/& gt;
& lt//service& gt;
& lt;服务名称=& quot; WcfContact.HealthPortalLoginService& quot; behaviorConfiguration =&"httpBehaviour&"& gt;
& lt;端点地址=& quot;& quot; binding =&"webHttpBinding&"合同=" WcfContact.IHealthPortalLoginService" behaviorConfiguration =&"httpEndpointBehaviour&"& gt;
& lt; identity& gt;
& lt; dns value =&本地主机& quot;/& gt;
& lt//identity& gt;
& lt//endpoint& gt;
<端点地址=&"mex&" binding =& quot; mexHttpBinding& quot; contract =&"IMetadataExchange&"/& gt;
& lt//service& gt;
& lt//services& gt;
& lt !!-& lt; bindings& gt;
& lt; webHttpBinding& gt;
& lt;绑定名称=&"StreamedRequestWebBinding&"
BypassProxyOnLocal =& quot; true& quot;
useDefaultWebProxy =& quot; false& quot;
hostNameComparisonMode =&"WeakWildcard&";
sendTimeout =& quot; 00:05:00& quot;
openTimeout =& quot; 00:05:00& quot;
receiveTimeout =& quot; 00:05:00& quot;
maxReceivedMessageSize =&"2147483647&"
maxBufferSize =&"2147483647&"
maxBufferPoolSize =&"2147483647&"
transferMode =&"StreamedRequest& gt;"
& lt; readerQuotas maxArrayLength =& quot; 2147483647& quot;
maxStringContentLength =&"2147483647&"/>
& lt//binding& gt;
& lt//webHttpBinding& gt;
& lt;/bindings& gt-& gt;
& lt;行为& gt;
& lt; endpointBehaviors& gt;
& lt !!-& lt;行为名称=& quot; jsonBehavior& quot;>
& lt; enableWebScript/& gt;
& lt//behavior& gt .--& gt;
& lt;行为名称=&"httpEndpointBehaviour& quot;"& gt;
& lt; webHttp/& gt;
& lt//behavior& gt;
& lt//endpointBehaviors& gt;
& lt; serviceBehaviors& gt;
& lt;行为名称=&"httpBehaviour& quot;<
& lt; serviceMetadata httpGetEnabled =&"true& quot; /& gt;
& lt; serviceDebug includeExceptionDetailInFaults =&"false&" /& gt;
& lt//behavior& gt;
& lt;行为名称=&&"& gt;
& lt; serviceMetadata httpGetEnabled =&"true& quot; /& gt;
& lt; serviceDebug includeExceptionDetailInFaults =&"false&" /& gt;
& lt//behavior& gt;
& lt;/serviceBehaviors& gt;
& lt//behaviors& gt;
& lt//system.serviceModel& gt;
& lt;启动& gt;
& lt; supportedRuntime version =& quot; v4.0& quot; sku =&.NETFramework,Version = v4.0&"/& gt;
& lt;/startup& gt;
& lt;/configuration& gt;</pre>


我可以从C#应用程序中使用该服务


HttpWebRequest请求=(HttpWebRequest)WebRequest.Create("http://xx.xx.xx.xx/ContactService/HealthPortalLoginService.svc/loging");

request.Method ="POST";
request.ContentType = @"application/json; charset = utf-8";

LoginDataContract local =新的LoginDataContract();
local.EmailAddress ="satya@suryahospitals.com";
local.Password =克里希纳";
local.CreatedDate = DateTime.UtcNow;


DataContractJsonSerializer jsonser = new DataContractJsonSerializer(typeof(LoginDataContract));
MemoryStream ms =新的MemoryStream();
jsonser.WriteObject(ms,local);


字符串json = Encoding.Default.GetString(ms.ToArray());

request.ContentLength = Encoding.UTF8.GetByteCount(json.ToString());

使用(Stream stream = request.GetRequestStream())
{
stream.Write(Encoding.UTF8.GetBytes(json.ToString()),0,Encoding.UTF8.GetByteCount(json.ToString()));

}


HttpWebResponse response = request.GetResponse()as HttpWebResponse;

流respStream = response.GetResponseStream();
如果(respStream!= null)
{
//lbl.Text = new StreamReader(respStream).ReadToEnd();
LoginDataContract p2 =(LoginDataContract)jsonser.ReadObject(respStream);
}


我的C#代码正在使用服务很好,
问题是,我的ipad开发人员无法使用我的服务,代码中是否有任何问题...

Hi

public interface IHealthPortalLoginService
{

[OperationContract]
[WebInvoke(UriTemplate = "/loging",Method="POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
LoginDataContract loging(LoginDataContract objlocal);

}
[DataContract]
public class LoginDataContract
{
#region Fields
[DataMember]

public int LoginId { get; set; }

[DataMember]

public string EmailAddress { get; set; }


[DataMember]

public string Password { get; set; }

[DataMember]

public int DoctorId { get; set; }

[DataMember]

public string Role { get; set; }


[DataMember]

public bool IsActive { get; set; }

[DataMember]

public DateTime CreatedDate { get; set; }

[DataMember]
public string FavoriteQuestion { get; set; }

[DataMember]
public string Answer { get; set; }

[DataMember]
public int Status { get; set; }

[DataMember]
public int LabId { get; set; }

[DataMember]
public int FrontdeskId { get; set; }

#endregion
}


Web.config


<pre lang="xml">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;configuration&gt;
&lt;appSettings&gt;
&lt;add key=&quot;DBProviderType&quot; value=&quot;1&quot;/&gt;
Password=PGI123;Pooling=true&quot;/&gt;
&lt;/appSettings&gt;
&lt;system.web&gt;
&lt;httpRuntime maxRequestLength=&quot;10000000&quot;/&gt;
&lt;compilation debug=&quot;true&quot;/&gt;
&lt;/system.web&gt;
&lt;!-- When deploying the service library project, the content of the config file must be added to the host''s
app.config file. System.Configuration does not support config files for libraries. --&gt;
&lt;system.serviceModel&gt;
&lt;serviceHostingEnvironment multipleSiteBindingsEnabled=&quot;true&quot; /&gt;
&lt;services&gt;
&lt;service name=&quot;WcfContact.Service1&quot; behaviorConfiguration=&quot;httpBehaviour&quot;&gt;
&lt;endpoint address=&quot;&quot; binding=&quot;webHttpBinding&quot; contract=&quot;WcfContact.IService1&quot; behaviorConfiguration=&quot;httpEndpointBehaviour&quot; &gt;
&lt;identity&gt;
&lt;dns value=&quot;localhost&quot;/&gt;
&lt;/identity&gt;
&lt;/endpoint&gt;
&lt;endpoint address=&quot;mex&quot; binding=&quot;mexHttpBinding&quot; contract=&quot;IMetadataExchange&quot;/&gt;
&lt;/service&gt;
&lt;service name=&quot;WcfContact.HealthPortalLoginService&quot; behaviorConfiguration=&quot;httpBehaviour&quot;&gt;
&lt;endpoint address=&quot;&quot; binding=&quot;webHttpBinding&quot; contract=&quot;WcfContact.IHealthPortalLoginService&quot; behaviorConfiguration=&quot;httpEndpointBehaviour&quot;&gt;
&lt;identity&gt;
&lt;dns value=&quot;localhost&quot;/&gt;
&lt;/identity&gt;
&lt;/endpoint&gt;
&lt;endpoint address=&quot;mex&quot; binding=&quot;mexHttpBinding&quot; contract=&quot;IMetadataExchange&quot;/&gt;
&lt;/service&gt;
&lt;/services&gt;
&lt;!--&lt;bindings&gt;
&lt;webHttpBinding&gt;
&lt;binding name=&quot;StreamedRequestWebBinding&quot;
bypassProxyOnLocal=&quot;true&quot;
useDefaultWebProxy=&quot;false&quot;
hostNameComparisonMode=&quot;WeakWildcard&quot;
sendTimeout=&quot;00:05:00&quot;
openTimeout=&quot;00:05:00&quot;
receiveTimeout=&quot;00:05:00&quot;
maxReceivedMessageSize=&quot;2147483647&quot;
maxBufferSize=&quot;2147483647&quot;
maxBufferPoolSize=&quot;2147483647&quot;
transferMode=&quot;StreamedRequest&quot;&gt;
&lt;readerQuotas maxArrayLength=&quot;2147483647&quot;
maxStringContentLength=&quot;2147483647&quot;/&gt;
&lt;/binding&gt;
&lt;/webHttpBinding&gt;
&lt;/bindings&gt;--&gt;
&lt;behaviors&gt;
&lt;endpointBehaviors&gt;
&lt;!--&lt;behavior name=&quot;jsonBehavior&quot;&gt;
&lt;enableWebScript/&gt;
&lt;/behavior&gt;--&gt;
&lt;behavior name=&quot;httpEndpointBehaviour&quot;&gt;
&lt;webHttp/&gt;
&lt;/behavior&gt;
&lt;/endpointBehaviors&gt;
&lt;serviceBehaviors&gt;
&lt;behavior name=&quot;httpBehaviour&quot;&gt;
&lt;serviceMetadata httpGetEnabled=&quot;true&quot; /&gt;
&lt;serviceDebug includeExceptionDetailInFaults=&quot;false&quot; /&gt;
&lt;/behavior&gt;
&lt;behavior name=&quot;&quot;&gt;
&lt;serviceMetadata httpGetEnabled=&quot;true&quot; /&gt;
&lt;serviceDebug includeExceptionDetailInFaults=&quot;false&quot; /&gt;
&lt;/behavior&gt;
&lt;/serviceBehaviors&gt;
&lt;/behaviors&gt;
&lt;/system.serviceModel&gt;
&lt;startup&gt;
&lt;supportedRuntime version=&quot;v4.0&quot; sku=&quot;.NETFramework,Version=v4.0&quot;/&gt;
&lt;/startup&gt;
&lt;/configuration&gt;</pre>


i can consume the service from c# app


HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://xx.xx.xx.xx/ContactService/HealthPortalLoginService.svc/loging");

request.Method = "POST";
request.ContentType = @"application/json; charset=utf-8";

LoginDataContract local = new LoginDataContract();
local.EmailAddress = "satya@suryahospitals.com";
local.Password = "krishna";
local.CreatedDate = DateTime.UtcNow;


DataContractJsonSerializer jsonser = new DataContractJsonSerializer(typeof(LoginDataContract));
MemoryStream ms = new MemoryStream();
jsonser.WriteObject(ms, local);


string json = Encoding.Default.GetString(ms.ToArray());

request.ContentLength = Encoding.UTF8.GetByteCount(json.ToString());

using (Stream stream = request.GetRequestStream())
{
stream.Write(Encoding.UTF8.GetBytes(json.ToString()), 0, Encoding.UTF8.GetByteCount(json.ToString()));

}


HttpWebResponse response = request.GetResponse() as HttpWebResponse;

Stream respStream = response.GetResponseStream();
if (respStream != null)
{
//lbl.Text = new StreamReader(respStream).ReadToEnd();
LoginDataContract p2 = (LoginDataContract)jsonser.ReadObject(respStream);
}


its fine that my c# code is consuming the service
Problem is ,my ipad developer cant consume my service , is ther any problem in my code...

推荐答案

在函数WCFJSON中将变量varProcessData设置为true可能应该是ProcessData您定义的全局变量.顺便说一句,这是糟糕的设计,应予以更改.构建一个包含这些参数的对象,然后将该配置对象传递给需要这些参数的方法.最好还是开发一个javascript类,该类执行您想要的操作并停止污染全局名称空间.

干杯!

-MRB
In function WCFJSON the variable varProcessData which is set to true should probably be ProcessData the global variable you defined. This is by the way bad design and should be changed. Build an object that contains these parameters and pass that configuration object to the methods that need these parameters. Better yet develop a javascript class that does what you want and stop polluting the global namespace.

Cheers!

-MRB


这篇关于消费wcf休息服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆