Exponse many class in one asmx Web Service - c#

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";
}
}
}

Related

Keeping the namespaces in c# soapservice

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.

Custom ASP.NET attribute not getting called

I have a custom Authorization attribute not getting called in my asp.net class and I can't tell what I'm missing.
My Attribute looks like this:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class MyAuthorize : System.Web.Http.AuthorizeAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
// ...
}
protected bool AuthorizeCore(HttpContextBase httpContext)
{
// Logic here.
return false;
}
}
And I'm calling it from inside a WebService like so:
[WebService(Namespace = "http://something/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AccessPoint : System.Web.Services.WebService
{
[WebMethod]
[MyAuthorize]
public bool SomeWebMethod(int a)
{
//Do stuff
return false;
}
}
Each time I run it, it will fall right through and never trigger the attribute.
FYI; I used System.Web.Http.AuthorizeAttribute because System.Web.Mvc.AuthorizeAttribute was telling me that AuthorizeAttribute did not exist.
You seem to be making a terrible confusion here between ASP.NET Web API and legacy classic ASMX WebServices. You have written an ASP.NET Web API authorization attribute (designed to be used in an ASP.NET API REST service) and applied it on a legacy ASMX WebService. Those two technologies are completely different and should not be mixed together.
It's like trying to put a Lamborghini engine on a horse driven cart.

Read authentication info (Principal) by developing a .Net Web Service

I am dealing with a Claim-based application, with the aim to display users info after their authentication through SSO.
For a given authenticated user, I realized a .Net Web Page wherein I show all claims starting from the provided Principal (Page.User), as follows:
public partial class ClaimsPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var claimsPrincipal = Page.User as IClaimsPrincipal;
if (claimsPrincipal != null)
{
IClaimsIdentity claimsIdentity = (IClaimsIdentity) claimsPrincipal.Identity;
// Something else...
}
}
}
And it works fine.
Now, I would like to do the same thing, but by using a .Net Web Service (SOAP), instead of the web page.
My current code is as follows:
[WebService(Namespace = "http://myapp/claims")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class ClaimsWS : System.Web.Services.WebService
{
[WebMethod]
public List<ClaimValues> GetClaims()
{
// Get Principal..
// Read claims...
}
}
My question is: how can I obtain the current Principal inside the WebService body?
Maybe you can try something along these lines. I am returning an Identity object, but you get the claims list from it also.
[WebService(Namespace = "http://myapp/claims")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class ClaimsWS : System.Web.Services.WebService
{
[WebMethod]
public List<ClaimValues> GetClaims()
{
var principal =ClaimsPrincipal.CreateFromHttpContext(HttpContext.Current);
return principal.Claims;
}
}

Cannot access ServiceHost methods

I have a Silverlight project that uses a WCF service that I created. My problem is that in my WCF service, I created a ServiceHost but VS2010 doesn't seem to recognize the instance of my object (underlines the svHost). Below is the code for my service.
using System;
using System.Collection.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
namespace userIO.Web
{
[ServiceContract]
public class CoordsService
{
[OperationContract]
public double xDir();
[OperationContract]
public double yDir();
[OperationContract]
public String keyPressed();
public class Coords : CoordsService
{
public double xDir { get; set; }
public double yDir { get; set; }
public String keyPressed { get; set; }
}
ServiceHost svHost = new ServiceHost(typeof(Coords), new Uri("http://localhost:8080"));
BasicHttpBinding binding = new BasicHttpBinding();
svHost.AddServiceEndpoint(typeof(CoordsService), binding, "");
svHost.Open();
}
}
Your ServiceContract should decorate an interface (a contract). The ServiceHost should host an instance of this interface and be outside the same service that it's hosting. At least I've only seen it done in this way.
The basic structure is:
[ServiceContract]
public interface IService
{
[OperationContract]
void DoSomething(Data data);
}
[DataContract]
public class Data
{
[DataMember]
int Num {get;set;}
}
public class Service : IService
{
public void DoSomething(Data data)
{ // do something }
}
// run in any other kind of app, console, win service, winform/wpf
static void Main()
{
ServiceHost svHost = new ServiceHost(typeof(Service), new Uri("http://localhost:8080"));
BasicHttpBinding binding = new BasicHttpBinding();
svHost.AddServiceEndpoint(binding, "");
svHost.Open();
}
An even easier solution to get your service up and running in VS2010 is to just create the service in a new WCF Service template. Take out their demo code, put in your own code for the servicecontract interface and the implementation service, then choose debug -> run and VS2010 will host the service for you without having to create an external app to run the service. Will also let you send data to the service to test out the code and the return values of your wcf functions in their simple winforms app.

How to access a web service with overloaded methods

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

Categories