WCF Hosting with IP Address

2019-08-31 00:50发布

问题:

I have created a WCF Service which return JSON data. here is my code:

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;
        }
}

my web.config is as follows:

    <?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>

Everything is working fine when i run this wcf on localhost as: localhost:13186/ServiceApp.svc/GetCities

problem: when i pass my IP address as 192.168.1.16:13186/ServiceApp.svc/GetCities

it is giving an error that website is too busy... & i want to access this wcf service in URL on other computers i mean other PCs on my Network. I have changed my web.config as per my requirements now if anyone know what to do next to host this service with IP address. or Host this into Microsoft 2003 Server SP2. please help..

回答1:

I'm assuming this is Cassini? If so - Cassini only responds if the host header is 'localhost' I think. Certainly you won't get it to respond by your IP.

Host it in IIS or IIS Express.

On a different note - if this is a .Net 4 project you might be interested to note that Rest services implemented this way via WCF is soon to become legacy - and be replaced by the Asp.Net Web API once it's gone to RTM (it's currently at RC stage) - I urge you to consider this newer technology if you can (it wouldn't solve this issue though).