2017-06-05 100 views
1

我已經從我的一個用戶那裏得到了WCF服務。我想檢查服務是否正常工作而不添加任何代理。有什麼方法可以在我的C#代碼中實現這一點?WCF服務沒有在C#中使用代理服務器

+0

使用ChannelFactory 。您將需要訪問服務程序集來執行此操作。 –

回答

0

您可以通過在WCF上實現端點並從客戶端查詢它來實現此目的。 以下是我將使用的WCF代碼。

// Used for communication between WCF and client. Must be implemented both WCF and client sides 
public class Response { 
public int Id { get; set; } 
public string Data { get; set; } 
} 

// Web Service - Interface 
[ServiceContract] 
public interface IService 
{ 
     [OperationContract] 
     [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, 
      UriTemplate = "Up")] 
     string CheckLogin(); 
} 

// Web service - Implementation 
public class ServiceImplementation : IService 
{ 
    public Response isUp() 
    { 
     Response response = new Response(); 
     response.ID = 200; 
     response.Data = "web service is up"; 
     return response; 
    } 
} 

以下是測試服務是否啓動的客戶端方法。

public bool CheckIfUp(string encodedUrl) 
{ 
    WebRequest request; 
    WebResponse ws; 
    Response response = new Response(); 
    string url = "http://servicePath/isUp"; // your wcf url 
    try 
     { 
       request = WebRequest.Create(url); 
       ws = request.GetResponse(); 
       return (response.ID == 200); 
     } 
     catch (Exception e) 
     { 
       Console.Write(e.StackTrace); 
     } 
      return false; 
} 

希望這會有所幫助。

+0

我無權訪問WCF服務代碼。我只有服務網址。 –

0

嘗試在指向WCF服務的URL處追加?wsdl

如果您的Web服務地址是

http://services.aonaware.com/DictService/DictService.asmx 

你可以這樣達到您的WSDL文件:

http://services.aonaware.com/DictService/DictService.asmx?WSDL 

返回的WSDL,您可以看到所有的WCF服務提供的方法。