2015-11-16 73 views
0

我正在尝试为我正在制作的网站测试我的WCF服务。我正在使用jQuery来调用服务($.getJSON()),但我一直在网站上收到Connection Refused Error错误。测试WCF时未列出服务

因此,我检出了部署到计算机时所做的网站,并且我的方法“GetData()”未列出。它只是列出服务本身的名称。我是相当新的一般使用WCF所以怜悯:')在测试客户端的Windows Studio中打开我的服务甚至没有列出:

Service List

,当我尝试添加它,它说该服务已成功添加,但没有显示。今天早些时候,我看到列表中的方法,但不得不删除整个项目,因为我搞砸了。

在Web.config看起来是这样的:

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

    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <!-- 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="true"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="EndpBehavior"> 
      <webHttp/> 
     </behavior> 
     <behavior name="enableScriptBehavior"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <services> 
     <service behaviorConfiguration="ServiceBehavior" name="PUendeligWebService.ExampleService"> 
     <endpoint address="" binding="webHttpBinding" 
        contract="PUendeligWebService.ExampleServiceInterface" behaviorConfiguration="EndpBehavior"/> 
     </service> 
    </services> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     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"/> 
    <httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*"/> 
     </customHeaders> 
    </httpProtocol> 
    </system.webServer> 

</configuration> 

接口:

[ServiceContract] 
public interface ExampleServiceInterface 
{ 
    [OperationContract] 
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
    String GetData(); 

    [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; } 
    } 
} 

服务:

public class ExampleService : ExampleServiceInterface 
{ 
    public String GetData() 
    { 
     Random ran = new Random(); 
     TestClass[] tc = new TestClass[5]; 
     TestClass tc1 = new TestClass(); 
     tc1.TheText = "First Text " + ran.Next(); 
     tc1.TheOtherText = "First Other Text " + ran.Next(); 
     TestClass tc2 = new TestClass(); 
     tc2.TheText = "Second Text " + ran.Next(); 
     tc2.TheOtherText = "Second Other Text " + ran.Next(); 
     TestClass tc3 = new TestClass(); 
     tc3.TheText = "Third Text " + ran.Next(); 
     tc3.TheOtherText = "Third Other Text " + ran.Next(); 
     TestClass tc4 = new TestClass(); 
     tc4.TheText = "Fourth Text " + ran.Next(); 
     tc4.TheOtherText = "Fourth Other Text " + ran.Next(); 
     TestClass tc5 = new TestClass(); 
     tc5.TheText = "Fifth Text " + ran.Next(); 
     tc5.TheOtherText = "Fifth Other Text " + ran.Next(); 

     tc[0] = tc1; 
     tc[1] = tc2; 
     tc[2] = tc3; 
     tc[3] = tc4; 
     tc[4] = tc5; 

     return JsonConvert.SerializeObject(tc); 
    } 

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

最后,只是良好的措施,这里是我的jQuery用途:

$(function() { 
    $("input:button").click(function() { 
     $.getJSON("http://localhost:52535/ExampleService.svc/GetData?callback=?", function(data) { 
      alert(data); 
     }); 
    }); 
}); 
+0

不特定的端口,没有。我正在使用Netbeans将网站部署到自己的端口上。 – OmniOwl

+0

尝试在** Release **而不是Debug中构建解决方案,然后再次运行。我在尝试使用WCF测试客户端时遇到了类似的问题,并且在我更改构建模式时提供了帮助,因为它显然需要发布构建中的.dll文件。 – urbz

+0

@urbz它没有工作。它具有完全相同的行为:/ – OmniOwl

回答

1

(想写一个评论,但它有很长......) 要创建一个简单的REST服务,你必须做以下几个步骤:

1)定义服务的方法和接口:

namespace CoreAuroraService.Services 
{ 
    [DataContract] 
    public class Patient 
    { 
     [DataMember] 
     public String LastName{get;set;} 

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

    [ServiceContract] 
    public interface IPatientService 
    { 
     [OperationContract] 
     [WebGet(UriTemplate = "/GetAllPatients", ResponseFormat = WebMessageFormat.Json)] 
     List<Patient> GetAllPatients(); 

     [OperationContract] 
     [WebInvoke(UriTemplate = "/Create", Method = "POST", ResponseFormat = WebMessageFormat.Json)] 
     bool CreatePatient(); 


     [OperationContract] 
     [WebInvoke(UriTemplate = "/Update", Method = "PUT", ResponseFormat = WebMessageFormat.Json)] 
     bool UpdatePatient(Guid patientGuid); 


     [OperationContract] 
     [WebInvoke(UriTemplate = "/Delete", Method = "DELETE", ResponseFormat = WebMessageFormat.Json)] 
     bool DeletePatient(Guid patientGuid); 
    } 

    public class PatientService : IPatientService 
    { 
     public List<Patient> GetAllPatients() 
     { 
      var patient = new Patient() { FirstName = "Jeem", LastName = "Street" }; 
      var patients = new List<Patient> { patient }; 
      return patients; 
     } 

     public bool CreatePatient() 
     { 
      // TODO: Implement the logic of the method here 
      return true; 
     } 

     public bool UpdatePatient(Guid patientGuid) 
     { 
      // TODO: Implement the logic of the method here 
      return true; 
     } 

     public bool DeletePatient(Guid patientGuid) 
     { 
      // TODO: Implement the logic of the method here 
      return true; 
     } 
    } 
} 

2)现在你必须定义你的服务的行为。要做到这一点,你必须改变你的服务项目中的配置文件。在这个文件中,您必须定义服务行为以及让您的服务保持平静的其他设置。要做到这一点贴下一代码serviceModel块内:

<services> 
    <service name="CoreAuroraService.Services.PatientService"> 
    <endpoint address="" behaviorConfiguration="rest" binding="webHttpBinding" bindingConfiguration="maxStream" contract="CoreAuroraService.Services.IPatientService"> 
     <identity> 
     <dns value="localhost"/> 
     </identity> 
    </endpoint> 
    <host> 
     <baseAddresses> 
     <add baseAddress="https://localhost/Services/PatientService.svc"/> 
     </baseAddresses> 
    </host> 
    </service> 
</services> 

<behaviors> 
    <endpointBehaviors> 
    <behavior name="rest"> 
     <webHttp helpEnabled="true"/> 
    </behavior> 
    </endpointBehaviors> 
    <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="true"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

3)现在让我们写这会从JavaScript调用我们的服务的方法:

<script> 
    function GetP() { 
     // Now I need to send cross domain request to the service because my services hosted in another project 
     // Tested in IE 11 
     var url = "http://localhost:29358/Services/PatientService.svc/GetAllPatients"; 
     $.ajax({ 
      type: 'GET', 
      dataType: "text", 
      url: url, 
      success: function (responseData, textStatus, jqXHR) { 
       console.log("in"); 
       var data = JSON.parse(responseData); 
       console.log(data); 
      }, 
      error: function (responseData, textStatus, errorThrown) { 
       alert('POST failed.'); 
      } 
     }); 
    } 
</script> 
+0

这看起来不错。我会接受它作为这个答案:) – OmniOwl

1

首先它会更好,如果你改变从这个webInvoke属性:通过这样写的网址

[OperationContract] 
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate="/getData")] 
String GetData(); 

不是运行你的服务,并在浏览器中打开它:

[OperationContract] 
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
String GetData(); 

对此这样的:

http://host_name:port_number/service_name.svc/getData 

之后,你应该让你的数据(如果everythink是确定)

而当我尝试添加它时,它说服务已成功添加 ,但没有显示。今天早些时候,我看到列表 中的方法,但不得不删除整个项目,因为我搞砸了。

我认为这是因为webHttpBinding(它使您的服务变得宁静)而发生的,这是您在Web配置文件中设置的。 Usualy测试客户端为SOAP服务生成调用方法。

+0

当我使用http:// localhost:52535/ExampleService.svc/GetData访问方法时,我得到一个页面,上面写着“The Method is not allowed”。如何更改我的配置,以便我可以回到SOAP服务而不是REST?使用该方法时,该网站也给我一个405错误。 – OmniOwl

+0

那么我有一些进展。我进入了我的WebInvoke属性并添加了“Method =”*“”,现在它可以工作。现在我只需要弄清楚如何解析我收到的JSON字符串。 – OmniOwl

+0

为此,您可以使用像这里解释的json.parse()方法http://www.mkyong.com/javascript/how-to-access-json-object-in-javascript/ – Tequila