2016-08-18 29 views
0

Ich habe einen Test WCF-Dienst, wenn ich dies auf Visual Studio versuchen funktioniert es gut, wenn ich den Link http://localhost:1347/Service1.svc/DoWork in den Browser einfügen zeigt es schlechte Anfrage mit leeren weißen Bildschirm des BrowsersWCF-Service zeigt leeren Bildschirm im Browser

fehlgeschlagen Ressource laden: der Server mit einem Status von 400 reagiert (Bad Request)

Wenn ich mit JSON versucht, es zeigt CORS Problem, habe ich ein Skript in Web.Config aber existieren noch die Problem der schlechten Anfrage, WCF hat folgende Betriebsverträge;

namespace WcfService1 
{ 

    [ServiceContract] 
    public interface IService1 
    { 

     [OperationContract] 
     string DoWork(); 

     [OperationContract] 
     string GetData(int value); 

     [OperationContract] 
     CompositeType GetDataUsingDataContract(CompositeType composite); 


    } 
    [DataContract] 
    public class CompositeType 
    { 
     bool boolValue = true; 
     string stringValue = "Hello "; 

     [DataMember] 
     public bool BoolValue 
     { 
      get { return boolValue; } 
      set { boolValue = value; } 
     } 

     [DataMember] 
     public string StringValue 
     { 
      get { return stringValue; } 
      set { stringValue = value; } 
     } 
    } 
} 

ihre Implementierung kommt hier.

public class Service1 : IService1 
    { 

     public string DoWork() 
     { 
      return string.Format("This is DoWork()"); 
     } 
     public string GetData(int value) 
     { 
      return string.Format("You entered: {0}", value); 
     } 

     public CompositeType GetDataUsingDataContract(CompositeType composite) 
     { 
      if (composite == null) 
      { 
       throw new ArgumentNullException("composite"); 
      } 
      if (composite.BoolValue) 
      { 
       composite.StringValue += "Suffix"; 
      } 
      return composite; 
     } 
    } 

Web.Config

<?xml version="1.0"?> 
<configuration> 

    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5.2"/> 
    <httpRuntime targetFramework="4.5.2"/> 
    <httpModules> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/> 
    </httpModules> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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> 
    </behaviors> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https"/> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <remove name="ApplicationInsightsWebTracking"/> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" 
     preCondition="managedHandler"/> 
    </modules> 

    <httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*"/> 
     <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" /> 
     <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" /> 
     <add name="Access-Control-Max-Age" value="1728000" /> 
     </customHeaders> 
    </httpProtocol> 

    <!-- 
     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"/> 
    <validation validateIntegratedModeConfiguration="false"/> 




    </system.webServer> 

</configuration> 

JSON Aufruf

$(document).ready(function(){ 
      $.get("http://localhost:1347/Service1.svc/DoWork", function(data, status){ 

       alert("Data: " + data + "\nStatus: " + status); 
     }); 

Antwort

0

Hinzufügen in Ihrem global.asax Datei folgenden Code ein. Dadurch können preflight Anforderungen erfolgreich abgeschlossen werden.

protected void Application_BeginRequest(object sender, EventArgs e) 
{ 
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
    if (HttpContext.Current.Request.HttpMethod == "OPTIONS") 
    { 
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); 
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); 
    HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); 
    HttpContext.Current.Response.End(); 
    } 
} 
+0

nichts passiert ist. http: // localhost: 1347/Service1.svc/DoWork zeigt weiterhin eine ungültige Anforderung im Browser an – Mangrio

+0

Kann den detaillierten Fehler posten. Siehe Netzwerk-Registerkarte in der Konsole. – Mairaj

+0

ein anderes ist XMLHttpRequest kann nicht laden http: // localhost: 1347/Service1.svc/DoWork. Der 'Access-Control-Allow-Origin'-Header enthält mehrere Werte' *, * ', aber nur einer ist erlaubt. Origin 'http: // localhost' ist daher nicht erlaubt. – Mangrio

0

Die "schlechte Anfrage" Antworten aufgrund des HTTP-Verbs Mismatch wären. SOAP-Anforderungen für alle Vorgänge mit BasicHttpBinding in WCF verwenden HTTP POST, nicht HTTP GET. Wenn Sie HTTP GET verwenden möchten, verwenden Sie webHttpBinding.

Oder noch besser, verwenden Sie Web-API anstelle von WCF.

Verwandte Themen