Reputation: 611
I'm trying to create a WCF web service with JSON and Consume with Client in ASP .NET My WCF web server is up and running hosted on IIS, I have checked with browser, getting JSON response.
Here is Server web.config file:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<!--<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>-->
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<services>
<service name="WcfServiceApp.Service1">
<endpoint address="../Service1.svc" binding="webHttpBinding" contract="WcfServiceApp.IService1" behaviorConfiguration="webBehaviour"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior >
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/>
</customHeaders>
</httpProtocol>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
After that I created a Web Application Client in ASP .NET to consume WCF web service. I have added WCF web service reference in Client, But
Visual Studio 2012 not updating client web.config
. I have found few things in stackoverflow for my client web.config
Client web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<!--<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>-->
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webby">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost/WCFService/Service1.svc" name="Service1" binding="webHttpBinding"
contract="ServiceReference1.IService1" behaviorConfiguration="webby"/>
</client>
</system.serviceModel>
</configuration>
calling Service from Client
protected void Button1_Click(object sender, EventArgs e)
{
string result;
string input = tbInput.Text;
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
try
{
result = client.GetData(input);
lbResult.Text = result;
client.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
But when I tried read from Web service, getting Exception
There was no endpoint listening at http://localhost/WCFService/Service1.svc/GetData that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Inner Exception: The remote server returned an error: (404) Not Found."}
I suspect its a Configuration issue in my web.config file,, But not sure what is causing this issue.
Thanks, Ashok
Upvotes: 2
Views: 2672
Reputation: 611
I read more about webHttpBinding, REST found Client for such services cannot be created with Add service reference.
Here is sample code to call WCF Service: ( I found it easy), response is in JSON format, you need to extract it differently (as of now I don't know how to do that)
Thanks to http://www.codeproject.com/Articles/275279/Developing-WCF-Restful-Services-with-GET-and-POST
string url = "http://localhost:50327/Service1.svc/data/"+input;
string strResult = string.Empty;
// declare httpwebrequet wrt url defined above
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
// set method as post
webrequest.Method = "GET";
// set content type
webrequest.ContentType = "application/json"; //x-www-form-urlencoded”;
// declare & read response from service
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
// set utf8 encoding
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
// read response stream from response object
StreamReader loResponseStream = new StreamReader
(webresponse.GetResponseStream(), enc);
// read string from stream data
strResult = loResponseStream.ReadToEnd();
// close the stream object
loResponseStream.Close();
// close the response object
webresponse.Close();
// assign the final result to text box
result = strResult;
lbResult.Text = strResult;
Upvotes: 1
Reputation: 562
I think it will not be always config problem: once I had the same problem but I put to forgot servicecontract
so service was running on the server but not able to access it.
here are some checkpoints:
1)find your .svc
file and right click on it and select option view in browser that will give you the exact local URL
.
2)give you contract parameter a full value e.g. instead of Contract1 add it with full namespace x.y.Contract1
3)check you have put the latest dll on the IIS server and reseted the app pool
4) check OperationContract ,ServiceContract tags are properly set or not
Upvotes: 0
Reputation: 3726
You're using WebHttpBinding
for your service.So basically that's an REST
service. So you cannot add Service Reference
because REST
services do not expose any metadata and you need to query the resources via HTTP
verbs like GET
or POST
or so.
Try like :
var req = (HttpWebRequest)WebRequest.Create("your endpoint");
var data_to_send = Encoding.ASCII.GetBytes("some data");
using (var _temp = req.GetRequestStream())
{
_temp.Write(data_to_send, 0, data_to_send.Length);
}
var res = req.GetResponse();
Alternatively you can choose:
var req = new HttpClient().PostAsync("your url", new StringContent(JsonConvert.SerializeObject("your params")));
Also enable a few properties in your endpointBehavior
to catch more specific exception details.
Upvotes: 2