jbatista jbatista - 1 year ago 60
C# Question

How to get a webservice's own endpoint in C#?

I have a C# webservice, as such:

namespace MyProject
#region "Frontend"

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyWsClass : System.Web.Services.WebService
private static readonly IRegisterer reg = new SelfRegisterInDatabase();

private MyWsClass()
// tell who I am to a database

// A service I expose to the users
public object MyMethod(object in)
// do some work
return new Object();


#region "Backend"

public interface IRegisterer
public void SelfRegister();

// Herein lies my question:
public class SelfRegisterInDatabase : IRegisterer
private static volatile bool IsRegistered = false;

public void SelfRegister()
// Expecting something like: "http://THEHOST:THEPORT/MyWebservice.aspx?WSDL"
// In principle, the variable part is "THEHOST:THEPORT".
string EndpointUrl =

// 1) open a Database connection;
// 2) insert new entry for this EndpointUrl string if doesn't exist;
// 3) and close DB connection
SelfRegisterInDatabase.IsRegistered = true;

I would like to know how can I dynamically read the webservice get its own endpoint so that I can have it register in a database (say, SQL Server -- the actual
ing is not what I'm asking about here). Which method/configuration should I use?

Answer Source

Try this to get the entire endpoint String:


You'll need

using System.ServiceModel.Web;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download