当我手动调用wcf服务时,我应该在url中输入什么:
HttpWebRequest httpWebRequest = WebRequest.Create(url)as HttpWebRequest;
应该是我的svc文件的URL
http://localhost/service/LMTService.svc
或wsdl
http://localhost/service/LMTService.svc?wsdl
或网址服务行动?
http://localhost/service/LMTService.svc/soap/GetSerializedSoapData
答案 0 :(得分:3)
这取决于端点的绑定。如果使用SOAP绑定(即basicHttpBinding
,wsHttpBinding
等),请求URI应该是端点地址(而不是服务地址)。此外,在某些SOAP版本(例如,basicHttpBinding
中使用的SOAP11)中,您需要将操作指定为HTTP标头。如果您正在使用webHttpBinding
(具有webHttp
行为),则地址是端点的地址,以及要调用的操作的UriTemplate(默认情况下只是方法名称)。 / p>
以下代码显示基于HttpWebRequest
的请求发送到两个端点,一个使用BasicHttpBinding
,一个使用WebHttpBinding
。
public class StackOverflow_7525850
{
[ServiceContract]
public interface ITest
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int Add(int x, int y);
}
public class Service : ITest
{
public int Add(int x, int y)
{
return x + y;
}
}
public static string SendRequest(string uri, string method, string contentType, string body, Dictionary<string, string> headers)
{
string responseBody = null;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.Method = method;
if (headers != null)
{
foreach (string headerName in headers.Keys)
{
req.Headers[headerName] = headers[headerName];
}
}
if (!String.IsNullOrEmpty(contentType))
{
req.ContentType = contentType;
}
if (body != null)
{
byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
req.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
req.GetRequestStream().Close();
}
HttpWebResponse resp;
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException e)
{
resp = (HttpWebResponse)e.Response;
}
if (resp == null)
{
responseBody = null;
Console.WriteLine("Response is null");
}
else
{
Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
foreach (string headerName in resp.Headers.AllKeys)
{
Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]);
}
Console.WriteLine();
Stream respStream = resp.GetResponseStream();
if (respStream != null)
{
responseBody = new StreamReader(respStream).ReadToEnd();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine("HttpWebResponse.GetResponseStream returned null");
}
}
Console.WriteLine();
Console.WriteLine(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ");
Console.WriteLine();
return responseBody;
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "basic");
host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "web").Behaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Host opened");
string soapBody = @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
<s:Body>
<Add xmlns=""http://tempuri.org/"">
<x>44</x>
<y>55</y>
</Add>
</s:Body>
</s:Envelope>";
SendRequest(baseAddress + "/basic", "POST", "text/xml", soapBody, new Dictionary<string, string> { { "SOAPAction", "http://tempuri.org/ITest/Add" } });
SendRequest(baseAddress + "/web/Add", "POST", "text/xml", "<Add xmlns=\"http://tempuri.org/\"><x>55</x><y>66</y></Add>", null);
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}