0

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 で次のように実行すると、すべて正常に動作します: localhost:13186/ServiceApp.svc/GetCities

問題: IP アドレスを 192.168.1.16:13186/ServiceApp.svc/GetCities として渡すと

Web サイトがビジー状態であるというエラーが表示されます... & 他のコンピューター (つまり、ネットワーク上の他の PC) の URL でこの wcf サービスにアクセスしたいです。IPアドレスでこのサービスをホストするために次に何をすべきかを知っている人がいれば、私の要件に従ってweb.configを変更しました。またはこれを Microsoft 2003 Server SP2 にホストします。助けてください..

4

1 に答える 1

1

これはカッシーニだと思いますか?もしそうなら - Cassini は、ホスト ヘッダーが「localhost」の場合にのみ応答すると思います。確かに、IP で応答することはありません。

IIS または IIS Express でホストします。

別の注意事項 - これが .Net 4 プロジェクトの場合、WCF を介してこのように実装された REST サービスは間もなくレガシーになり、RTM に移行するとAsp.Net Web APIに置き換えられることに注意してください (現在 RC 段階にあります) - 可能であれば、この新しいテクノロジを検討することを強くお勧めします (ただし、この問題は解決しません)。

于 2012-07-31T10:48:46.547 に答える