NET 4.0中带有WCF的JSON

编程入门 行业动态 更新时间:2024-10-25 14:23:55
本文介绍了NET 4.0中带有WCF的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个应该返回JSON的WCF应用程序. 我的服务合同如下所示

i have a WCF application which is supposed to return JSON. my service contract is like below

[ServiceContract(Name = "MobileTV", Namespace = "MobileTVWCF")] public interface IMobileTVService { [OperationContract] string GetData(int value); [OperationContract] [WebGet(UriTemplate = "GetDataUsingDataContract/{deviceID}", ResponseFormat = WebMessageFormat.Json)] Customer GetDataUsingDataContract(int deviceID); } [DataContract] public class Customer { [DataMember] public int CustomerID { get; set; } [DataMember] public string Name { get; set; } [DataMember] public bool IsActive { get; set; } [DataMember] public string MobileNo { get; set; } [DataMember] public bool IsAuthenticated { get; set; } }

而我的服务实现就像波纹管一样 [AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 公共类MobileTVService:IMobileTVService { 公共字符串GetData(int值) { 返回string.Format(您输入的是{0}",值); } 公用客户GetDataUsingDataContract(int deviceID) { 客户cust =新客户(); SqlParameter [] paramsToStore =新的SqlParameter [1]; paramsToStore [0] =新的SqlParameter("@ deviceID",SqlDbType.Int); paramsToStore [0] .Value = deviceID; SqlDataReader sqlDataReaderObj = SqlHelper.ExecuteReader(@数据源= 192.168.205.71 \ Development;初始目录= PCTV;用户ID = sa;密码= s @ 123",CommandType.StoredProcedure,StoredProcedures.GetCustomerDetails,paramsToStore); 如果(sqlDataReaderObj.HasRows) { var customSqlDatareder = new CustomSqlDataReader(sqlDataReaderObj); while(customSqlDatareder.Read()) { cust.CustomerID = Convert.ToInt32(customSqlDatareder.GetString("intCustomerId")); cust.MobileNo = customSqlDatareder.GetString("vcMobileNumber"); cust.Name = customSqlDatareder.GetString("Name"); cust.IsActive = customSqlDatareder.GetBoolean("boolISActive"); } } 其他 { cust = null; } 返回客户; } } 我已经在< system.servicemodel> 的web.config中进行了一些设置

and my service Implemetation is as bellow [AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class MobileTVService : IMobileTVService { public string GetData(int value) { return string.Format("You entered: {0}", value); } public Customer GetDataUsingDataContract(int deviceID) { Customer cust = new Customer(); SqlParameter[] paramsToStore = new SqlParameter[1]; paramsToStore[0] = new SqlParameter("@deviceID", SqlDbType.Int); paramsToStore[0].Value = deviceID; SqlDataReader sqlDataReaderObj = SqlHelper.ExecuteReader(@"Data Source=192.168.205.71\Development;Initial Catalog=PCTV;User ID=sa;Password=s@123", CommandType.StoredProcedure, StoredProcedures.GetCustomerDetails, paramsToStore); if (sqlDataReaderObj.HasRows) { var customSqlDatareder = new CustomSqlDataReader(sqlDataReaderObj); while (customSqlDatareder.Read()) { cust.CustomerID = Convert.ToInt32(customSqlDatareder.GetString("intCustomerId")); cust.MobileNo = customSqlDatareder.GetString("vcMobileNumber"); cust.Name = customSqlDatareder.GetString("Name"); cust.IsActive = customSqlDatareder.GetBoolean("boolISActive"); } } else { cust = null; } return cust; } } i have done some setting in web.config in <system.servicemodel>

<system.serviceModel> <!--<serviceHostingEnvironment aspNetCompatibilityEnabled="True"></serviceHostingEnvironment>--> <services> <service behaviorConfiguration="MobileTVWCFTest.Service1Behavior" name="MobileTVWCFTest.MobileTVService"> <endpoint address="" binding="wsHttpBinding" contract="MobileTVWCFTest.IMobileTVService" behaviorConfiguration="MobileTVWCFTest.Service1EndBehavior"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="MobileTVWCFTest.Service1Behavior"> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="MobileTVWCFTest.Service1EndBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel>

任何人都可以告诉我,在我运行代码时我在哪里做错COZ引发异常 "localhost:1515/MobileTVService.svc"上的终结点没有带有None MessageVersion的绑定. "System.ServiceModel.Description.WebHttpBehavior"仅适用于WebHttpBinding或类似的绑定.

can any one tell me that where i m doing wrong coz when i am running my code it throwing exception The endpoint at ''localhost:1515/MobileTVService.svc'' does not have a Binding with the None MessageVersion. ''System.ServiceModel.Description.WebHttpBehavior'' is only intended for use with WebHttpBinding or similar bindings

推荐答案

我找到了答案,所以我在这里发布.代码已写入,但我在< system.servicemodel>中具有的设置web.config中的标记不正确. 设置如下:- i found the answer so i am posting here. code was write but the setting i have in <system.servicemodel> tag in web.config was incorrect. setting is as follows:-- <system.serviceModel> <!--<serviceHostingEnvironment aspNetCompatibilityEnabled="True"></serviceHostingEnvironment>--> <services> <service behaviorConfiguration="MobileTVWCFTest.Service1Behavior" name="MobileTVWCFTest.MobileTVService"> <endpoint address="" binding="webHttpBinding" contract="MobileTVWCFTest.IMobileTVService" behaviorConfiguration="MobileTVWCFTest.Service1EndBehavior"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="MobileTVWCFTest.Service1Behavior"> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="MobileTVWCFTest.Service1EndBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel>

更多推荐

NET 4.0中带有WCF的JSON

本文发布于:2023-11-09 05:26:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571504.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   JSON   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!