本文介绍了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引发异常
"http://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 ''http://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>
    <!--<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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 14:40