我创建了一个返回 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>
当我在本地主机上运行此 wcf 时,一切正常:localhost:13186/ServiceApp.svc/GetCities
问题:当我将我的IP 地址传递为 192.168.1.16:13186/ServiceApp.svc/GetCities
它给出了一个错误,即网站太忙了……我想在其他计算机上的 URL 中访问这个 wcf 服务,我的意思是我网络上的其他 PC。如果有人知道接下来要做什么来使用 IP 地址托管此服务,我现在已根据我的要求更改了我的 web.config。或将其托管到 Microsoft 2003 Server SP2 中。请帮忙..