2015-10-14 37 views
-1

我開發了一個wcf service.it在我的本地工作正常時,消費service.but發生了什麼這是servce發佈在server.then每當使用此服務得到400個不好request.please幫助我。使用wcf服務時的400次badrequest?

下面

是我的代碼:

namespace SampleWcfproject 
{ 
    [ServiceContract] 
    public interface ISampleService1 
    { 
     [OperationContract] 
     //[WebInvoke(Method="GET",ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped,UriTemplate="getdata")] 
     [WebInvoke(UriTemplate = "/getdata/{uuid}", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
     string getdata(string uuid); 

     [OperationContract] 
     string getcities(); 
    } 
} 

sampleservice.svc.cs:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class SampleService1 : ISampleService1 
{ 
     public string getdata(string uuid) 
     { 
      try 
      { 


       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con2"].ConnectionString); 
       SqlCommand comm = new SqlCommand("select CircleID, CircleName from Circle", con); 
       con.Open(); 
       comm.ExecuteNonQuery(); 

       SqlDataAdapter da = new SqlDataAdapter(comm); 
       // DataSet ds = new DataSet(); 
       DataTable dt = new DataTable(); 
       da.Fill(dt); 

       JavaScriptSerializer JSSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
       List<Dictionary<string, object>> DtRows = new List<Dictionary<string, object>>(); 
       Dictionary<string, object> newrow = null; 

       //Code to loop each row in the datatable and add it to the dictionary object 
       foreach (DataRow drow in dt.Rows) 
       { 
        newrow = new Dictionary<string, object>(); 
        foreach (DataColumn col in dt.Columns) 
        { 
         newrow.Add(col.ColumnName.Trim(), drow[col]); 
        } 
        DtRows.Add(newrow); 
       } 
       con.Close(); 


       return JSSerializer.Serialize(DtRows); 
      } 
      catch (Exception ex) 
      { 

       List<String> Parameters = new List<String>(); 
       Parameters.Add("getRechargeCircleList"); 

      // SendErrorMail(ex.ToString(), "getRechargeCircleList", Parameters); 
       // Context.Response.ContentType = "application/json; charset=utf-8"; 
       // Context.Response.Write(@" {""Status"":""false"",""Message"" : ""Oops! Something went Wrong""}"); 
       // return; 
      } 
      return "false"; 
     } 
    } 
} 

web.config文件:

<system.web> 

    <httpRuntime maxRequestLength="32768"/> 
    <compilation debug="true" targetFramework="4.0"/> 
    <customErrors mode="Off"/> 
    <!-- 
     The <authentication> section enables configuration 
     of the security authentication mode used by 
     ASP.NET to identify an incoming user. 
    --> 
    <authentication mode="Windows"/> 
    <!-- 
     The <customErrors> section enables configuration 
     of what to do if/when an unhandled error occurs 
     during the execution of a request. Specifically, 
     it enables developers to configure html error pages 
     to be displayed in place of a error stack trace. 

     <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> 
     <error statusCode="403" redirect="NoAccess.htm" /> 
     <error statusCode="404" redirect="FileNotFound.htm" /> 
     </customErrors> 
    --> 
    <pages controlRenderingCompatibilityVersion="4.0" clientIDMode="AutoID"/> 
    </system.web> 
    <!-- 
     The system.webServer section is required for running ASP.NET AJAX under Internet 
     Information Services 7.0. It is not necessary for previous version of IIS. 
    --> 
    <system.webServer> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 
    <system.serviceModel> 

    <bindings> 
    <basicHttpBinding> 
     <binding name="basicHttp" allowCookies="true" 
       maxReceivedMessageSize="4194304" 
       maxBufferSize="20000000" 
       maxBufferPoolSize="20000000"> 
      <readerQuotas maxDepth="32" 
       maxArrayLength="200000000" 
       maxStringContentLength="200000000"/> 
     </binding> 
    </basicHttpBinding> 
</bindings> 
    <services> 
     <service behaviorConfiguration="SampleWcfproject.Service1Behavior" name="SampleWcfproject.Service1"> 
     <endpoint address="" binding="basicHttpBinding" contract="SampleWcfproject.IService1"> 

      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 

     <endpoint address="" binding="netTcpBinding" contract="SampleWcfproject.IService1"/> 

      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
     <service behaviorConfiguration="SampleWcfproject.SampleService1Behavior" name="SampleWcfproject.SampleService1"> 
     <endpoint address="basic" binding="basicHttpBinding" contract="SampleWcfproject.ISampleService1"> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="web"> 
       <dataContractSerializer maxItemsInObjectGraph="2147483647"/> 
      </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="SampleWcfproject.Service1Behavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     <behavior name="SampleWcfproject.SampleService1Behavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

實際上發生了什麼這是服務工作的罰款我的.net項目。我通過使用添加服務引用和正常獲取數據來添加此服務。但每當我是我們時然後,HttpWebRequest發出了400個壞請求,我該怎麼做。我不知道發生了什麼。

更新時間:

我得到在我的Windows Phone應用程序的響應。但android的人得到錯誤。他們正在得到html response.i.e。,我的wcf調試瀏覽器頁面。

+0

<端點地址= 「」 ..沒有定義 –

+0

@KishoreSahas如何定義的終點地址以及 –

+0

@KishoreSahas終點地址的裝置「http://sdnmfdjffjdfidfj/SampleService1.svc」url? –

回答

1

請修改Service Model Configuration這樣

刪除<endpoint address="" binding="netTcpBinding" contract="SampleWcfproject.IService1"/>

<service behaviorConfiguration="SampleWcfproject.Service1Behavior" name="SampleWcfproject.Service1"> 

    <endpoint address="" binding="basicHttpBinding" contract="SampleWcfproject.IService1"> 

    <identity> 
<dns value="localhost"/> 
    </identity> 
    </endpoint> 

    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
    </service> 
+0

我正在創建wcf service.this服務消費給andriod people.but他們越來越error.they正在獲取html頁面。 –

+0

Plz草擬一個新的,我會檢查它,並在這裏刪除評論。 – Webruster

+0

我不會更改任何代碼。我得到了我的Windows Phone應用程序的響應。但android人得到error.they得到html response.that html設計是我的wcf web服務調試頁面。 –