どのようにWCF webHttpの動作はHEAD動詞を受け入れる作るには?
-
21-09-2019 - |
質問
私は、WindowsサービスでホストされているWCFサービスを提供しています。 私はそれをHEADリクエストを送信するたびに、問題は、私はHTTP 405を取得されます。
、それにwebHttpの動作とするたびに、私はそれを私が欲しいものであるGETリクエストI GET HTTP 200を送信してwebHttpBindingを追加しました作るための方法は、それがHEADのためにも、200を返します。httpありますか? それも可能ですか?
編集:操作の契約です。
[OperationContract]
[WebGet(UriTemplate = "MyUri")]
Stream MyContract();
解決
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate="/data")]
string GetData();
}
public class Service : IService
{
#region IService Members
public string GetData()
{
return "Hello";
}
#endregion
}
public class Program
{
static void Main(string[] args)
{
WebHttpBinding binding = new WebHttpBinding();
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:9876/MyService"));
host.AddServiceEndpoint(typeof(IService), binding, "http://localhost:9876/MyService");
host.Open();
Console.Read();
}
}
上記のコードは正常に動作します。私は、HEAD要求に405(許可されていないメソッド)を取得します。アセンブリ私が使用しているのバージョンは、Version = 3.5.0.0、文化=中立、なPublicKeyToken = 31bf3856ad364e35ます。
、System.ServiceModel.Webです実は私の知る限りでは、それはないになり、これは、各メソッドのニーズがGETこととHEADのために行われなければならbelow..But it.Howeverあなたが解決策のようなものを試みることができる可能のないまっすぐ進むべき道は、ありませんそのエレガントな解決策..
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "*", UriTemplate = "/data")]
string GetData();
}
publicクラスのサービス:IService { #region IServiceメンバー
public string GetData()
{
HttpRequestMessageProperty request =
System.ServiceModel.OperationContext.Current.IncomingMessageProperties["httpRequest"] as HttpRequestMessageProperty;
if (request != null)
{
if (request.Method != "GET" || request.Method != "HEAD")
{
//Return a 405 here.
}
}
return "Hello";
}
#endregion
}
他のヒント
サービス(あるいは枠組み)に深刻なバグのように聞こえます。 HTTP / 1.1のHEADのサポートは、決してオプションである。
所属していません StackOverflow