[英]Can I call a WCF endpoint from ASP.Net Web API?
我有一個帶有使用 rest http(post/get 調用)的控制器方法的 web api。 客戶端通過普通的 httpclient 調用使用這個 API,我將一個 http 響應傳回。
我的用例是我有一個需要調用另一台服務器的遺留方法。 此方法目前使用 WCF 和合同綁定,但我不想在此 API 項目中使用 WCF。
有沒有辦法我仍然可以只使用 WEB API 調用這些方法,或者我必須混合架構(Web api 和 WCF)?
這是正常的方法調用
首先我初始化代理
var proxy = GetAccountProxy(); public static AcountClient GetAccountProxy() { var client = new AccountClient(); client.ClientCredentials.ClientCertificate.SetCertificate(...); return client; }
我通過代理連接到另一台服務器上的方法
var accountInfo = proxy.GetAccountInfo(xmlAccount);
public string AccountInfo(string sXml){ AccountLookup val = new AccountLookup(); val.Body = new AccountLookupRequestBody(); val.Body.XML = sXML; AccountLookupResponse retVal = ((AccountLookupResponse)(this)).AccountLookup(val); return retVal;
}
在我的 webconfig 中,端點看起來像這樣
<endpoint address="https://www.mylookup.com/AccountLookupWS/AccountLookupWS.svc/wshttp" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAccountLookupWS" contract="AccountLookupWS.IAccountLookupWS" name="WSHttpBinding_IAccountLookupWS1" />
所以我的問題是我可以使用普通的 rest httpclient 調用來調用這個端點並獲得相同的結果嗎?
Uri baseUrl = new Uri("https://www.mylookup.com/AccountLookupWS/AccountLookupWS.svc/wshttp");
IRestClient client = new RestClient(baseUrl);
IRestRequest request = new RestRequest("GetAccountInfo", Method.GET)
request.AddParameter("XmlAccount", sXml);
IRestResponse<dynamic> response = client.Execute<dynamic>(request);
if (response.IsSuccessful)
{
response.Data.Write();
}
else
{
Console.WriteLine(response.ErrorMessage);
}
Console.WriteLine();
一般來說,可以通過webrequest訪問wcf,但這取決於wcf服務的實現。 查看 WebGet 和/或 WebInvoke 屬性https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.web.webgetattribute?view=netframework-4.8並從那里開始。 這是在 WCF 端完成的工作,正確完成后,您可以像正常的休息服務一樣訪問您的 wcf。
這取決於您的 WCF 服務器綁定。 你看,HTTP/s 協議實現只是 WCF 的可能模塊,它的一部分,就像任何其他協議一樣 - 它只是稱為binding 。 不同的綁定意味着相同的綁定應該在客戶端,否則他們不會相互理解。
例如,如果服務器告訴:
然后客戶端應該反過來做同樣的事情。 這就是 WCF,它的靈活性為研究人員和開發人員打開了地獄之門。
正如我所說,如果你的服務器支持 HTTP 綁定,沒有額外的東西 - 你很好。 使用 http 客戶端或十億其他 HTTP 類。 如果沒有 - 將您的服務器協議綁定移植到NET Core並使用它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.