WCF 服务在本地工作,但在服务器上提供 404

问题描述:

我看过很多关于这个的帖子,但似乎无法真正找到答案.

I've seen a number of posts about this, but can't seem to actually find an answer.

[免责声明:我对 WCF 了解不多 -- 虽然我可以访问代码,但这是我试图连接到的现有项目.]

[Disclaimer: I don't know a lot about WCF -- Although I have access to the code, this is an existing project that I'm trying to connect to.]

我创建了一个 html/javascript 客户端来访问现有的 WCF 服务 API.每个都是一个单独的应用程序,所以当我在本地运行时,客户端和服务器都在 localhost 上运行,但端口号不同.这有效.客户端应用程序能够通过如下调用调用 WCF 服务:https://localhost:4435/sessions.svc/getsession?sessionPK=3.

I've created a html/javascript client to access an existing WCF Service API. Each is a separate application so when I run locally, the client and server are both running on localhost but with different port numbers. This works. The client app is able to call a WCF service with a call like: https://localhost:4435/sessions.svc/getsession?sessionPK=3.

当我在服务器上安装这两个应用程序时(都在同一台服务器上作为不同的虚拟应用程序运行),该服务确实在运行——我可以点击 https://myserver.com/api/sessions.svc 来自网络浏览器,我从服务返回一个页面,告诉我它正在工作.

When I install both apps on a server (both running on the same server as different virtual applications), the service is indeed running -- I can hit https://myserver.com/api/sessions.svc from a web-browser and I get a page back from the service telling me that its working.

但是,现在当客户端尝试以与本地相同的方式访问服务时 (https://myserver.com/api/sessions.svc/getsession?sessionPK=3),我收到 404 Not Found 错误.

However, now when the client tries to access the service the same way that it did locally (https://myserver.com/api/sessions.svc/getsession?sessionPK=3), I get a 404 Not Found error.

客户端和 WCF 服务都托管在具有相同根域的同一台服务器上——因此 CORS 应该不是问题.

Client and WCF Service are both hosted on the same server with the same root domain -- so CORS shouldn't be an issue.

以下是一些代码片段:

** WCF 服务 **

** WCF Service **

[ServiceContract]
public interface ISessions
{
    [OperationContract]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    string GetSession(int? sessionPK);

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Sessions : BaseService, ISessions
    {
        public string GetSession(int sessionPK)
        {
          ...
        }
}

** 服务的 Web.Config **

** Web.Config for Service **

<service behaviorConfiguration="BehaviorConfig" name="Sessions">
  <endpoint address="" binding="webHttpBinding" behaviorConfiguration="json"  contract="ISessions">
    <identity>
      <dns value="localhost"/>
    </identity>
  </endpoint>
  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>

** 客户 **

export class DataService {

   constructor(private http : HttpClient){
     this.http.configure(config => {
      config.withBaseUrl("https://myserver.com/api/");
      config.withHeader("Accept", "application/json");
      config.withHeader("Content-Type", "application/json");
      config.withCredentials(true);
    })
  }

  public async getSessionById(id : number = 0){
    try{
       let result = this.http.get(`sessions.svc/getsession?sessionPK=${id}`);
       if(!result.isSuccess)
          throw response;

       ...
    }
    catch(error : any){
       ...
    }
}

更新

响应 Peter,我从 https://myserver.com/api/sessions.svc 得到了与从 https://myserver.com/api 得到的相同的答复/sessions.svc?wsdl.页面是这样的:

In response to Peter, I get the same response from https://myserver.com/api/sessions.svc as I do from https://myserver.com/api/sessions.svc?wsdl. It is a page like this:

该服务似乎没有返回它公开的操作列表.顺便说一句,它在 localhost 上的执行方式完全相同.

The service doesn't seem to return a list of the operations it exposes. Incidentally, it performs exactly the same way on localhost.

原来,我在本地运行 HTTP,在服务器上运行 HTTPS.我通过添加以下内容解决了我的问题:

Turns out, I was running HTTP locally and HTTPS on the server. I resolved my issue by adding the following:

 <webHttpBinding>
   <binding>
     <security mode="Transport"></security>
   </binding>
 </webHttpBinding>