I have this web service:
using System.Web.Services;
namespace Contracts
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Second : System.Web.Services.WebService
{
[WebMethod]
public string GetContract()
{
return "Contract 1";
}
}
}
The following WPF application can display the HTML fine:
using System.Windows;
using System.Net;
using System;
namespace TestConsume2343
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
WebClient proxy = new WebClient();
proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted);
proxy.DownloadStringAsync(new Uri("http://localhost:57379/Second.asmx?op=GetContract"));
Message.Text = "loading...";
}
void proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Message.Text = e.Result.ToString();
}
}
}
but when I replace the above line with the actual web service:
proxy.DownloadStringAsync(new Uri("http://localhost:57379/Second.asmx/GetContract"));
it gives me the error message:
The remote server returned the error (500) Internal Server Error.
Although when I look at the same URL in my browser, I see the XML text fine:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Contract 1</string>
Why is it giving me the 500 error?
Turn on tracing and logging on your WCF service to see what the exception is (MSDN link) or attach the debugger with break on every exception as already suggested. .
The service returning HTTP code 500 usually means that there was an exception during request processing. If you have debugger attached to the web server and configured properly it will break on exception and you will be able to see it. Also usually the text of the exception should be included in the body of the HTTP response
Related
I am trying to set up a asmx service to return json inside the web method. When I view the response inside
a browser instead of returning json I get xml. I have set up my web method to return the json format.
Please find my web service below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
namespace space_port_lander_real_app
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod(CacheDuration = 60)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
//string xmlstring = "<user><name>Hello World</name><password>some pass</password></user>";
//var name = $(xml).find('name').text();
//return xmlstring;
return "hello world";
}
}
}
The response output looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">hello world</string>
I set up my method to return a string. Is there something I'm missing?
If you want a GET method to return JSON, you need to set it otherwise use POST.
[ScriptMethod(UseHttpGet=true, ResponseFormat = ResponseFormat.Json)]
Check Here
I need to build an API using C# webservice, which needs to return values in json format.
Currently I have the following lines of code
namespace WebService1
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return new JavaScriptSerializer().Serialize(new { errMsg = "test" });
}
}
}
The output of this when the post method is invoked is
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"errMsg":"test"}</string>
But this isn't a valid json how do I make the webservice return the json object only and not the xml headers?
Calling this webservice with header Content-Type: application/json will automatically transform the response to json and your xml will be gone.
I'm working through two tutorials to create a super simple WCF web service and Silverlight app.
Buiding a Service
Accessing a Service from Silverlight
Everything was going fine. I created my service:
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
namespace TestOnline.Web.Data
{
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DataService
{
[OperationContract]
public String TestService()
{
return "Service Worked!";
}
}
}
I added it as a service reference, then tried to create an instance but I'm getting the error "Cannot create an instance of the abstract class or interface" on the line "proxy = new DataService();"
I pretty much followed the steps of the tutorial exactly, I'm unsure what I've missed. I've certainly not seen many Service examples with constructors, and the reference code is auto-generated - so I don't want to go adding them manually to that.
Does anyone know of a solution/what I've done wrong? Thanks
using System.ServiceModel;
using TestOnline.ServiceReference1;
namespace TestOnline
{
public partial class MainPage : UserControl
{
DataService proxy;
public MainPage()
{
InitializeComponent();
proxy = new DataService();
}
private void TestServiceButton_Click(object sender, RoutedEventArgs e)
{
//call service and get response
}
}
}
You should be creating an instance of the generated proxy client class.
It'll be named DataServiceClient() if it's been added correctly.
I'm posting below message from soap UI and i always receiving null value in webservice. Whatever message I'm posting from soap UI it is taking as null only.
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
Test
</env:Body>
</env:Envelope>
Below is my simple WebService
[WebService(Namespace = "http://test.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class TestService : System.Web.Services.WebService {
[WebMethod]
public int TestMethod(string message)
{
try
{
File.WriteAllText("D:\\abc.xml", message);
return 0;
}
catch (Exception ex)
{
return 1;
}
}
}
You're not specifying which method to call or the parameters of that method in your body. You'll need something like this:
<env:Body>
<m:TestMethod xmlns:m="http://tempuri.org">
<message>Test</message>
</m:TestMethod>
</env:Body>
I'm trying to publish my web service, i have already changed Build Action properties of .asmx file to "Compile" and Web.config to "None" but still i get following errors when im trying to rebuild solution:
Error 1 Keyword, identifier, or string expected after verbatim specifier: # \MyVirtualCartWS\MyVirtualCartWS.asmx
Error 2 A namespace cannot directly contain members such as fields or methods \MyVirtualCartWS\MyVirtualCartWS.asmx
both errors points .asmx file so here it is:
<%# WebService Language="C#" CodeBehind="MyVirtualCartWS.asmx.cs" Class="MyVirtualCartWS.MyVirtualCartWS" %>
Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections;
namespace MyVirtualCartWS
{
/// <summary>
/// Summary description MyVirtualCartWS
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class MyVirtualCartWS : System.Web.Services.WebService
{
#region Using LoggedUser
[WebMethod]
public LoggedUser AddUser(string _login, string _email, string _password)
{
LoggedUser lUser = new LoggedUser();
lUser.AddUser(_login, _email, _password);
return lUser;
}
[WebMethod]
public LoggedUser LogIn(string _login, string _password)
{
LoggedUser lUser = new LoggedUser();
lUser.LogIn(_login, _password);
return lUser;
}
#endregion
}
}
The line:
[System.Web.Script.Services.ScriptService]
should be uncomment first.