I created WCF service and calling it from the android studio and its working fine, but after implementing WCF perSession functionality it is working for a single user at a time.
Problem:
My problem is when i am hitting WCF url with multiple user my sessionID get overwrite by the latest logged in user, So how to maintain session for multiple user like we do in web application.
I used this to creste session in WCF:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
and this is my method to create sessionid within services class
public class MyService : IMyService
{
static string currentSessionID = string.Empty;
public string createSession()
{
currentSessionID = DateTime.Now.Ticks.ToString();
return currentSessionID;
}
public string Login()
{
var mysessionId = createSession();
return mysessionId;
}
public string Mymethods(string data)
{
string response = "";
if(data.StartsWith("01"))
response = Login();
return response;
}
}
and am hitting this createSession() method only in login function.
Please help me out of this.....
Thanks in advnance.
Related
I want to create a C# application that communicates with two SOAP webservices. These webservices (WSDL files) use the same url
<root>/...dirPath.../dms.cfc?wsdl
<root>/...dirPath.../cobra.cfc?wsdl
<root> should be dynamic because the application user has to set this variable.
First of all I took this
How can I dynamically switch web service addresses in .NET without a recompile?
and tried this
https://www.codeproject.com/Articles/12317/How-to-make-your-Web-Reference-proxy-URL-dynamic
Further I found this link
https://learn.microsoft.com/en-us/sql/reporting-services/report-server-web-service/net-framework/setting-the-url-property-of-the-web-service?view=sql-server-2017
but these links didn't help I can't find the settings URL behaviour and I can't access the URL property by code.
I created a static class that should handle both webservices. The user is able to change the webservice root url.
An example URL would be
http://localhost:8500/CoBRA/...dirPath.../dms.cfc?wsdl
or
http://myInstance.com/CoBRA/...dirPath.../dms.cfc?wsdl
handled by this code
public static class CoBRAService
{
private static cobraClient cobraBaseClient = new cobraClient();
private static dmsClient cobraDmsClient = new dmsClient();
public static void SetWebserviceRootUrl(string rootUrl)
{
// cobraBaseClient.url = $"{rootUrl}/path/dms.cfc?wsdl";
// cobraDmsClient.url = $"{rootUrl}/path/cobra.cfc?wsdl";
}
}
Both webservices don't inherit from System.Web.Services.Protocols.SoapHttpClientProtocol they implement this public partial class cobraClient : System.ServiceModel.ClientBase<MyProject.CoBRA_Base.cobra>, MyProject.CoBRA_Base.cobra
This is my project structure
Where can I set the webservice url or how can I access the url property?
if your "CoBRA_BaseClient" and "CoBRA_DMSClient" inherited from System.ServiceModel.ClientBase< TChannel >
then you can try the following:
public static CoBRA_BaseClient CreateService()
{
CoBRA_BaseClient service = new CoBRA_BaseClient();
service.Endpoint.Address = new EndpointAddress("uri");
return service;
}
public static CoBRA_DMSClient CreateService()
{
CoBRA_DMSClient service = new CoBRA_DMSClient();
service.Endpoint.Address = new EndpointAddress("uri");
return service;
}
I have got a WCF service from one of my users. I want to check whether the service is working or not without adding any proxy. Is there any way that I can achieve this in my C# code?
You may achieve this by implementing an endpoint at WCF and querying it from the client.
Following is the WCF code I would use.
// Used for communication between WCF and client. Must be implemented both WCF and client sides
public class Response {
public int Id { get; set; }
public string Data { get; set; }
}
// Web Service - Interface
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
UriTemplate = "Up")]
string CheckLogin();
}
// Web service - Implementation
public class ServiceImplementation : IService
{
public Response isUp()
{
Response response = new Response();
response.ID = 200;
response.Data = "web service is up";
return response;
}
}
Following is the client method to test if the service is up.
public bool CheckIfUp(string encodedUrl)
{
WebRequest request;
WebResponse ws;
Response response = new Response();
string url = "http://servicePath/isUp"; // your wcf url
try
{
request = WebRequest.Create(url);
ws = request.GetResponse();
return (response.ID == 200);
}
catch (Exception e)
{
Console.Write(e.StackTrace);
}
return false;
}
Hope this helps.
Try appending ?wsdl at the of the URL pointing to the WCF-service.
If your Web service address is
http://services.aonaware.com/DictService/DictService.asmx
you can reach your wsdl file like this:
http://services.aonaware.com/DictService/DictService.asmx?WSDL
The returned WSDL allows you to see all the method the WCF-service provides.
I've got a web application created with ASP.NET and a windows native client program written in c#.
The windows native program needs to send and fetch data from the ASP.NET web application.
I guess in the web application I'll need a controller for the external calls. And in the client Software I somehow Need to call them.
Is there a way to achieve calls with complex data types (lists of classes) as parameters?
How do I secure the calls from the client? Simple http-logon?
for example I'd like to transfer an instance of this class to or from the ASP.NET web application:
public class Address
{
public String Street {get;set;}
public String City {get;set;}
}
public class CustomerInformation
{
public String No {get;set;}
public String Name {get;set;}
public List<Address> Addresses {get;set;}
}
Of course the Windows client is running somewhere local while the ASP.NET Service is running in the web.
I would add API controller and put some methods there. For instance
// Addresses API
public class AddressController : ApiController
{
private readonly IRepository<Address> _repository;
public AddressController(IRepository<Address> repository)
{
_repository = repository;
}
[BasicAuthorize]
public IList<Address> GetList()
{
return _repository.GetAll();
}
}
// Constomer information API
public class CustomerInformationController : ApiController
{
private readonly IRepository<CustomerInformation> _repository;
public CustomerInformationController(IRepository<CustomerInformation> repository)
{
_repository = repository;
}
[BasicAuthorize]
public IList<CustomerInformation> GetList()
{
return _repository.GetAll();
}
}
To secure those methods you can use Basic authentication. This means that you can add authorization header for each request:
For example how it looks for user "myuser" with password "test"
Authorization: basic bXl1c2VyOnRlc3Q=
// Custom attribute for Basic authentication
public class BasicAuthorizeAttribute : System.Web.Http.AuthorizeAttribute
{
private readonly string[] _permissionNames;
public BasicAuthorizeAttribute()
{
}
public BasicAuthorizeAttribute(params string[] permissionNames)
{
_permissionNames = permissionNames;
}
protected override bool IsAuthorized(HttpActionContext actionContext)
{
// check if user has been already authorized
if (base.IsAuthorized(actionContext))
return true;
var user = AuthenticateUser(actionContext);
// here you can check roles and permissions
return user != null;
}
private IUser AuthenticateUser(HttpActionContext context)
{
var request = context.Request;
AuthenticationHeaderValue authHeader = request.Headers.Authorization;
if (authHeader != null)
{
// RFC 2617 sec 1.2, "scheme" name is case-insensitive
if (authHeader.Scheme.Equals("basic", StringComparison.OrdinalIgnoreCase) && authHeader.Parameter != null)
return AuthenticateUser(authHeader.Parameter);
}
return null;
}
private IUser AuthenticateUser(string credentials)
{
try
{
// parse values
var encoding = Encoding.GetEncoding("iso-8859-1");
credentials = encoding.GetString(Convert.FromBase64String(credentials));
var credentialsArray = credentials.Split(':');
var username = credentialsArray[0];
var password = credentialsArray[1];
// authentication
var membershipService = new IMembershipService();
return membershipService.ValidateUser(username, password);
}
catch (Exception)
{
// Credentials were not formatted correctly.
return null;
}
}
}
On client side you can use HttpClient to send async request
public async Task<Address[]> GetAddresses() {
var client = new HttpClient {BaseAddress = new Uri(_settingsService.GetHost())};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var base64 = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "myuser", "test")));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",base64);
HttpResponseMessage response = await client.GetAsync("api/addresses");
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(response.ReasonPhrase);
string content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Address[]>(content);
}
Is there a way to achieve calls with complex data types (lists of classes) as parameters?
Yes, The server application as ASP.NET or ASP.NET MVC or (preferably) ASP.NET WEB API can provide services with complex data types. In fact there is no limitation in declaring methods.
How do I secure the calls from the client? Simple http-logon?
There are wide ranage of authentication and authorization mechanism in ASP.NET (MVC, WEB API) which give you opportunity to choose one them.
The data transfers between your client and server via XML or JSON.
The "WebClient" class provides everything that you need to make a call from client to server.
More information:
http://www.codeproject.com/Articles/33798/HTTP-GET-with-NET-WebClient
How to post data to specific URL using WebClient in C#
How do I log into a site with WebClient?
I am working on a soap webservice in c#. It look like this:
public class MyService : System.Web.Services.WebService
{
public MyService()
{
}
[WebMethod]
public string Hello()
{
return "hello";
}
}
and I added a service reference to this web service from another website, so I can access the Hello() method from there using the code:
MyServiceSoapClient client = new MyServiceSoapClient();
client.Hello();
Now I need to pass the credentials to that web service. I have tried:
MyServiceSoapClient client = new MyServiceSoapClient();
client.ClientCredentials.UserName.UserName = "test";
client.ClientCredentials.UserName.Password = "pwd";
client.Hello();
But I could not manage to get these credentials in the webservice ( in the Hello() method).
How can I get these values in the webservice?
You get the user via the WebService.User property. This will give you the username but there is no way to retrieve the passed password, this is by design as the authentication happens at the IIS level before your WebService is run.
public class MyService : System.Web.Services.WebService
{
public MyService()
{
}
[WebMethod]
public string Hello()
{
return "hello, my name is " + User.Identity.Name;
}
}
I have now something like this:
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string Method1()
{
SomeObj so = SomeClass.GetSomeObj(); //this executes very long time, 50s and more
return so.Method1(); //this exetus in a moment
}
[WebMethod]
public string Method2()
{
SomeObj so = SomeClass.GetSomeObj(); //this executes very long time, 50s and more
return so.Method2(); //this exetus in a moment
}
...
}
Is it possible to make stateful web service so that I can reuse SomeObj so and just call methods on the same object?
So the client which will use this service would first call web method which would create so object and return some ID.
And then in subsequent calls the web service would reuse the same so object based on ID.
EDIT
Here is my actual code:
[WebMethod]
public List<ProcInfo> GetProcessList(string domain, string machineName)
{
string userName = "...";
string password = "...";
TaskManager tm = new TaskManager(userName, password, domain, machineName);
return tm.GetRunningProcesses();
}
[WebMethod]
public bool KillProcess(string domain, string machineName, string processName)
{
string userName = "...";
string password = "...";
(new TaskManager(userName, password, domain, machineName);).KillProcess(processName);
}
Stateful web services are not scalable and I wouldn't recommend them. Instead you could store the results of expensive operations in the cache. This cache could be distributed through custom providers for better scalability:
[WebMethod]
public string Method1()
{
SomeObj so = TryGetFromCacheOrStore<SomeObj>(() => SomeClass.GetSomeObj(), "so");
return so.Method1(); //this exetus in a moment
}
[WebMethod]
public string Method2()
{
SomeObj so = TryGetFromCacheOrStore<SomeObj>(() => SomeClass.GetSomeObj(), "so");
return so.Method2(); //this exetus in a moment
}
private T TryGetFromCacheOrStore<T>(Func<T> action, string id)
{
var cache = Context.Cache;
T result = (T)cache[id];
if (result == null)
{
result = action();
cache[id] = result;
}
return result;
}
Option 1
You can use your HttpSession.
//this executes very long time, 50s and more, but only once.
private SomeObj SessionSomeObj {
get
{
var ret = (SomeObj)Session["SomeObjStore"] ?? SomeClass.GetSomeObj();
SessionSomeObj = ret;
return ret;
}
set { Session["SomeObjStore"] = value; }
}
[WebMethod(EnableSession = true)]
public string Method1()
{
return SessionSomeObj.Method1(); //this exetus in a moment
}
[WebMethod(EnableSession = true)]
public string Method2()
{
return SessionSomeObj.Method2(); //this exetus in a moment
}
Note that this will only work if one call per client is made at a time.
Option 2
You can leave the class as is but use the WebMethod differently. If you are calling from a .Net generated class, async methods are provided for these occurrences. Basically you invoke the Method1 begin request method and get a call back when the execution is finished. You might need to tweak the timeout parameter of the web service client class for this to work though.
Option 3
You can use the caching features of the SixPack library to do this effortlessly! ;-)
[Edited after comment] There are now two static fields in option 1 to allow two different instances, one per method, as requested.
[Edited after further explanation] Using Session to make the calls stateful.
See: http://msdn.microsoft.com/en-us/library/aa480509.aspx
Also added Option 3.
Change the ServiceContract of your interface into:
[ServiceContract(SessionMode = SessionMode.Required)]
And put the following attribute on your class:
[ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.PerSession)]
See http://msdn.microsoft.com/en-us/library/system.servicemodel.sessionmode.aspx for more information and an example.