【问题标题】:WCF Hosting with IP Address带有 IP 地址的 WCF 托管
【发布时间】:2012-07-29 02:33:53
【问题描述】:

我创建了一个返回 JSON 数据的 WCF 服务。 这是我的代码:

namespace AppServices
{
[ServiceContract]
public interface Service1
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetCities", BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<City> GetCityCode();
}
[DataContract]
public class City
{
[DataMember]
public string CityId { get; set; }
[DataMember]
public string CityName { get; set; }
[DataMember]
public string StateId { get; set; }
[DataMember]
public string Priority { get; set; }
}
}

public class ServiceAPI : Service1
    {
 public List<City> GetCityCode()
        {
            adp = new SqlDataAdapter("Select * from tblCity", offcon);
            adp.Fill(ds, "City");
            var city = (from DataRow dr in ds.Tables["City"].Rows
                        select new
                        {
                            Id = dr["intCityId"].ToString(),
                            Name = dr["strTitle"].ToString(),
                            sid = dr["intStateId"].ToString(),
                            priority = dr["intPriority"].ToString()
                        }).Select(x => new City() { CityId = x.Id, CityName = x.Name, StateId = x.sid, Priority = x.priority }).ToList();

            return city;
        }
}

我的web.config如下:

    <?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceBehaviour" name="PatrikaAPIService.PatrikaService">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
                  contract="PatrikaAPIService.IPatrikaService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment/>
  </system.serviceModel>
</configuration>

当我在 localhost 上运行此 wcf 时,一切正常: 本地主机:13186/ServiceApp.svc/GetCities

问题: 当我将我的 IP 地址 传递为 192.168.1.16:13186/ServiceApp.svc/GetCities

它给出了一个错误,即网站太忙... &我想在其他计算机上的 URL 中访问此 wcf 服务,我的意思是我网络上的其他 PC。 如果有人知道接下来要做什么来使用 IP 地址托管此服务,我现在已根据我的要求更改了我的 web.config。 或将其托管到 Microsoft 2003 Server SP2 中。 请帮忙..

【问题讨论】:

  • 你得到了什么http响应?是 500(服务器忙)吗?
  • 不,它无法建立与服务器的连接.. 但与 localhost 合作得很好......请查看我编辑的问题..
  • 那么很可能是url中的IP地址不正确。
  • 不,我检查过它是正确的。
  • 默认情况下端口不应该监听任何http流量。当您使用您的 IP 地址时,您的流量将发送到 IIS 上的端口 80,该端口映射了 http 端口。尝试使用netsh并将13186映射到http端口,然后看看它是否有效

标签: wcf localhost ip-address


【解决方案1】:

我假设这是卡西尼?如果是这样 - 我认为 Cassini 仅在主机标头是“localhost”时才会响应。当然你不会让它通过你的 IP 来响应。

将其托管在 IIS 或 IIS Express 中。

另一方面 - 如果这是一个 .Net 4 项目,您可能有兴趣注意到通过 WCF 以这种方式实现的 Rest 服务很快就会成为遗留服务 - 并且一旦转到 RTM 就会被 Asp.Net Web API 取代(它目前处于 RC 阶段)- 如果可以,我敦促您考虑这种新技术(虽然它不会解决这个问题)。

【讨论】:

  • 以及如何做到这一点,因为我对此不太了解。行为。我对这项技术很陌生...
  • 在 IIS 中托管此服务后问题得到解决,实际上在我的情况下,它采用 ASP.NET 框架池的经典,我试图用集成模式来做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 2015-01-04
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多