パブリック (外部呼び出し可能) JSON データ フィードを ASP.net (4) Forms Web サイトに追加したいと考えています。このために、次の Web サービスを作成しました。
[WebService(Namespace = "http://localtest.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class BlogWebService : System.Web.Service.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Blog> GetLatestBlogs(int noBlogs)
{
return GetLatestBlogs(noBlogs)
.Select(b => b.ToWebServiceModel())
.ToList();
}
}
開いてローカルサーバーでこれをテストしました
http://localhost:55671/WebServices/BlogWebService.asmx?op=GetLatestBlogs
そしてそれは正しく動作します。
このサービスにリモートでアクセスしようとすると、内部サーバー エラーが発生します。たとえば、LinqPad を使用して次のコードを実行しました ( http://geekswithblogs.net/JuanDoNeblo/archive/2007/10/24/json_in_aspnetajax_part2.aspxのスクリプトに基づく):
void Main()
{
GetLatestBlogs().Dump();
}
private readonly static string BlogServiceUrl =
"http://localhost:55671/BlogWebService.asmx/GetLatestBlogs?noBlogs={0}";
public static string GetLatestBlogs(int noBlogs = 5)
{
string formattedUri = String.Format(CultureInfo.InvariantCulture,
BlogServiceUrl, noBlogs);
HttpWebRequest webRequest = GetWebRequest(formattedUri);
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string jsonResponse = string.Empty;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
jsonResponse = sr.ReadToEnd();
}
return jsonResponse;
}
private static HttpWebRequest GetWebRequest(string formattedUri)
{
Uri serviceUri = new Uri(formattedUri, UriKind.Absolute);
return (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);
}
いくつかの質問/疑問があります:
- Web サービスへの呼び出しはどのようにフォーマットする必要がありますか? LinqPad テスト コードでの BlogServiceUrl の構成が正しいかどうかはわかりません。
- BlogWebService クラスと GetBlogs() メソッドは正しく定義され、正しく属性付けされていますか?
- これを機能させるには、Web サイトの構成に何か追加する必要がありますか?
アドバイスをいただければ幸いです。