I'm trying to have overloaded methods in a web service but I am getting a System.InvalidOperationException when attempting "Add Web Reference" in Visual Studio 2005 (here's the relevant snippets of code):
public class FileService : System.Web.Services.WebService
{
private static readonly MetaData[] EmptyMetaData = new MetaData[0];
public FileService()
{
// a few innocent lines of constructor code here...
}
[WebMethod(MessageName = "UploadFileBasic",
Description = "Upload a file with no metadata properties")]
public string UploadFile(string trimURL
, byte[] incomingArray
, string fileName
, string TrimRecordTypeName)
{
return UploadFile(trimURL
, incomingArray
, fileName
, TrimRecordTypeName
, EmptyMetaData);
}
[WebMethod(MessageName = "UploadFile",
Description = "Upload a file with an array of metadata properties (Name/Value pairs)")]
public string UploadFile( string trimURL
, byte[] incomingArray
, string FileName
, string TrimRecordTypeName
, MetaData[] metaDataArray)
{
// body of UploadFile function here
I thought supplying a different MessageName property on the WebMethod attribute would fix this problem but here is the entire error message I get:
Both System.String UploadFileBasic(System.String, Byte[], System.String, System.String) and System.String UploadFile(System.String, Byte[], System.String, System.String) use the message name 'UploadFileBasic'. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.
The web service compiles OK; I cannot see what is wrong here.
My suggestion is to not use overloaded method names. There is no such concept in WSDL, so why bother?
You need to change this part:
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
to this one:
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
I would generally have a class object behind the web service interface that has the overloaded methods and then create individual methods in your asmx.cs file with different names. I know you can use the attributes but it just makes tidier code IMHO.
Operation overloading is not allowed for web services. But you can also follow the below steps.
Firstly you need to change
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
To
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
Secondly MessageName property of WebMethod should be different for Overloaded method.
namespace foo
{
/// <summary>
/// Summary description for TestService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[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 TestService : System.Web.Services.WebService
{
[WebMethod(MessageName = "HelloWorld1")]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod(MessageName = "HelloWorld2")]
public string HelloWorld(string Value = "default")
{
return "Hello World";
}
}
}
But if you will call URL Like:
http://localhost:15558/TestService.asmx/HelloWorld2?Value=2
It will work.
But if you will call URL Like:
http://localhost:15558/TestService.asmx/HelloWorld?Value=2
It will display HTTP 500
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 guess it is a stupid question but I'm havin a hard time finding something like it, so maybe someone here can push me in the right direction.
Problem:
I have a c# SoapService with lots of request/response classes, so let us say:
[WebService(Namespace = "http://fuu.bar.gov/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Main : System.Web.Services.WebService
{
[SoapHeader("authentication")]
[WebMethod(Description = "Get Foo")]
public FooResponse GetFoo(FooRequest request)
{
return new FooResponse ();
}
[SoapHeader("authentication")]
[WebMethod(Description = "Get Bar")]
public BarResponse GetBar(BarRequest request)
{
return new FooResponse ();
}
}
public class FooResponse{};
public class FooRequest{};
public class BarResponse{};
public class BarRequest{};
So when I create a WebReference named "foobarWebservice" in my client code I'll get:
foobarWebservice.FooResponse
foobarWebservice.FooRequest
foobarWebservice.BarResponse
foobarWebservice.BarRequest
class definitions.
What I'd like to is change the above into this:
public class Main : System.Web.Services.WebService
{
[SoapHeader("authentication")]
[WebMethod(Description = "Get Foo")]
public Foo.Response GetFoo(Foo.Request request)
{
return new FooResponse ();
}
[SoapHeader("authentication")]
[WebMethod(Description = "Get Bar")]
public Bar.Response GetBar(Bar.Request request)
{
return new FooResponse ();
}
}
which is easy:
namespace Foo
{
public class Response{}
public class Request{}
}
namespace Bar
{
public class Response{};
public class Request{};
}
But also the client Reference.cs should give me this:
foobarWebservice.Foo.Response
foobarWebservice.Foo.Request
foobarWebservice.Bar.Response
foobarWebservice.Bar.Request
So I'd be able to do that:
foobarWebservice.Main webservice = new foobarWebservice.Main();
foobarWebservice.Foo.Request newRequest = new foobarWebservice.Foo.Request();
foobarWebservice.Foo.Response response = webservice.GetFoo(newRequest);
Simply spoken: I want to keep my namespaces intact in the generated Reference.cs file on the client application.
Create a new assembly with the namespaces and use it in both client and server projects. In client project when adding the WCF reference check the 'use existing assemblies' option and check the added reference project/assembly.
Project A: Common classes
Project B: WCF service
Project C: WCF client. Make sure to check the reuse assemblies option.
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 have an university project where I should implement a java powered website that uses web services: some that will be created as servlets and others that should be created as .NET "servlets". I created the java servlet that can be called as /loginservice/username="__________"&md5="____________". All good.
Now I must implement another service in .NET. I created a ASP.NET web service application but this type of application uses POST instead of GET. I found out that this could be changed by adding
[ScriptMethod(UseHttpGet=true)]
but the problem is that I can't pass parameters like I do in Java.
There is no way of using POST anywhere because I don't want to rewrite the code in Java.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
namespace t5_services
{
/// <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 "Hello World";
}
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public string Package(String packagename, String lastname)
{
return "Hello " + packagename + ": " + lastname;
}
}
}
Here is the code in C#
If I use the browser and manually insert the values all is ok.
But I can't use the GET convention.
Thank you in advance.
I finally solve the problem by removing
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
and adding
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
to Web.config
Now I can call the service using
http://localhost:2586/Service1.asm/HelloWorld?parameter1=abc¶meter2=cde
This is an example of how I do it in WCF. I'm sure it will be very similar for your Asp.net service. If nothing else, it should point you in the right direction.
This is your function declaration in your interface file.
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "DoWork?message={message}&message2={message2}")]
string DoWork(string message, string message2);
This goes into a class that implements that interface.
public string DoWork(string message, string message2)
{
return "foobar";
}
Your GET request would then look something like http://yoursite.com/DoWork?message=param1&message2=param2
Does this help? Sorry for the short reply, just packing up to go home.
$.ajax & passing data to .asmx webservice
Its possible expose many class in single asmx in Web Service C#, this for generate
one Proxy class, and consume proxy from client like: Proxy.UserService.User and Proxy.ImageService.GetImage
I try this but dont Work.
namespace ServiciosWeb
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Services : System.Web.Services.WebService
{
}
public class ImageService : Services.IService
{
[WebMethod]
public string GetImage()
{
return "Image";
}
}
public class UserService : Services.IService
{
[WebMethod]
public string User()
{
return "User";
}
}
}
A classic ASMX web service is a class that needs to derive from System.Web.Services.WebService and decorated with the [WebService] attribute. If you want to expose multiple services you might need to have multiple ASMX files. Another possibility is to simply put the two web methods inside the existing service class so that when you generate the proxy class on the client they will be visible.
no that is not possible with [WebService] and [WebMethod]s.
You can not bind multiple classes to a single asmx, what you could do however -if you want to offer a multitude of class-files- is use the same class name like partial class. Would need to check to see if each class needs to be derived from IService, but I've did this in a project and it worked.
namespace ServiciosWeb
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public partial class Services : System.Web.Services.WebService
{
}
public partial class Services : Services.IService
{
[WebMethod]
public string GetImage()
{
return "Image";
}
}
public partial class Services : Services.IService
{
[WebMethod]
public string User()
{
return "User";
}
}
}