1

I made wcf rest service by going New->projects->WCF Service Application I am unable to use methods in console application while i have hosted wcf rest service and referenced wcf rest service in application My Application code is below :
IRestServiceImpL

    [ServiceContract]
        public interface IRestServiceImpL
        {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, 
        RequestFormat = WebMessageFormat.Xml, UriTemplate = "XmlData/{id}")]
        string XmlData(string id);

        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, 
        RequestFormat = WebMessageFormat.Json, UriTemplate = "JsonData/{id}")]
        string JsonData(string id);
        }



RestServiceImpL.svc.cs

[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
    public class RestServiceImpL : IRestServiceImpL
    {
       public string XmlData(string id)
       {
            return "you requested for " + id;
        }

        public string JsonData(string id)
        {
            return "you requested for " + id;
        }
}



Config File

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
    <bindings>
        <webHttpBinding>
            <binding name="StreamedRequestWebBinding"
                     bypassProxyOnLocal="true"
                     useDefaultWebProxy="false"
                     hostNameComparisonMode="WeakWildcard"
                     sendTimeout="10:15:00"
                     openTimeout="10:15:00"
                     receiveTimeout="10:15:00"
                     maxReceivedMessageSize="2147483647"
                     maxBufferSize="2147483647"
                     maxBufferPoolSize="2147483647"
                     transferMode="StreamedRequest"
                     crossDomainScriptAccessEnabled="true"
             >
                <readerQuotas maxArrayLength="2147483647"
                              maxStringContentLength="2147483647" />
            </binding>
        </webHttpBinding>
    </bindings>
    <services>
        <service name="RestService.RestServiceImpL" behaviorConfiguration="ServiceBehaviour">
            <!--<endpoint address="" binding="basicHttpBinding"  contract="RestService.IRestServiceImpL"></endpoint>-->
            <endpoint address="" binding="webHttpBinding" name="StreamedRequestWebBinding" bindingConfiguration="StreamedRequestWebBinding"  contract="RestService.IRestServiceImpL" behaviorConfiguration="web"></endpoint>
        </service>
    </services>

    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehaviour">
                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="true"/>
                <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="web">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*"/>
        </customHeaders>
    </httpProtocol>
    <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>



I host this service application in IIS

Now after giving reference to console application when i call its methods by using proxy class then i got error of Invalid Operation Contract Exception that endpoint not specified

calling code is below :

ServiceClient oServiceClient = new ServiceClient();<br/>
oServiceClient.JsonData("123");


Please suggest what is problem in the code.

4

1 回答 1

3

感谢堆栈溢出的支持...我做到了.. 调用 Wcf Rest Service 代码如下所示:

//code for xml Response consumption from WCF rest Service[Start]
WebRequest req = WebRequest.Create(@"http://RestService.com/WcfRestService/RestServiceImpL.svc/XmlData/sad");
req.Method = "GET";
req.ContentType = @"application/xml; charset=utf-8";
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
if (resp.StatusCode == HttpStatusCode.OK)
{
    XmlDocument myXMLDocument = new XmlDocument();
    XmlReader myXMLReader = new XmlTextReader(resp.GetResponseStream());
    myXMLDocument.Load(myXMLReader);
    Console.WriteLine(myXMLDocument.InnerText);
}
//code for xml Response consumption from WCF rest Service[END]

//****************************************************************************
//code for json Response consumption from WCF rest Service[Start]
WebRequest req2 = WebRequest.Create(@"http://RestService.com/WcfRestService/RestServiceImpL.svc/JsonData/as");
req2.Method = "GET";
req2.ContentType = @"application/json; charset=utf-8";
HttpWebResponse response = (HttpWebResponse)req2.GetResponse();
string jsonResponse = string.Empty;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
    jsonResponse = sr.ReadToEnd();
    Console.WriteLine(jsonResponse);
}
//code for json Response consumption from WCF rest Service[END]
于 2012-12-28T16:15:48.737 回答