WCF service session is always NULL - wcf

I have a WCF service. In global.asax, I put some data into the ASP.NET session. But when I call the WCF method, the Session object is always NULL.
This is my WCF service
[WebMethod(EnableSession = true)]
public List<Menu> GetMenus()
{
List<Menu> menulist = new List<Menu>();
Object[] O = HttpContext.Current.Session["menu"] as Object[];
foreach (var item in O)
{
Menu menu = new Menu();
menu.Html = ((WcfServices.Menu)(item.ToType(typeof(Menu)))).Html;
menu.Label = ((WcfServices.Menu)(item.ToType(typeof(Menu)))).Label;
menulist.Add(menu);
}
return menulist;
}
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
List<Menu> GetMenus();
Here is my global.asax file
protected void Session_Start(object sender, EventArgs e)
{
CrmHelper helper = new CrmHelper();
if (Session["service"] == null)
{
IOrganizationService s = helper.CreateService(true);
Session["service"] = s;
}
MobileHelper mobilehelper = new MobileHelper((IOrganizationService)Session["service"]);
if (Session["menu"] == null)
{
Session["menu"] = mobilehelper.GetMainMenus();
}
}
Accessing Session["service"] or Session["menu"] always returns null when I call the WCF service.
Any ideas?

The main issue is: WCF is NOT ASP.NET !
By default, WCF does NOT rely on the ASP.NET runtime (and that's a Good Thing!!) and thus doesn't have access to the ASP.NET constructs like Session ...
If you need to provide some data to a WCF service, best place to put that info is a database table where the WCF service can load the information from.
If you insist on using ASP.NET session storage, and you don't mind that you're limiting yourself to using only IIS with ASP.NET as the hosting environment for your WCF service in that case, check out this blog post showing exactly what you need to do to gain access to the ASP.NET session state.

Related

Castle Windsor - Resolving duplex wcf service

I'm hosting a duplex wcf service using windows service with castle windsor wcffacility using TCP binding.
There is no problem with hosting, I think, when I add a service reference to a console application.I'm able to access the duplex service without any issues.
Problem arises when I use castle windsor at the client side while resolving. Below is the code am using for adding the wcf services through code based on config file.
public static IWindsorContainer RegisterWcfClients(IocBuildSettings iocBuildSettings,
IWindsorContainer container)
{
//Register callback methods for duplex service first.
container.Register(Component.For<INotificationCallback>()
.ImplementedBy<NotificationCallbackCastle>()
.LifestyleTransient());
// get dictionary with key = service class, value = service interface
var servicesWithWcfInterfaces = Assembly.GetAssembly(typeof (IApplicationService))
.GetTypes()
.Where(x => (x.IsInterface || x.IsClass) && HasServiceContract(x))
.ToList();
var registrations = new List<IRegistration>();
//get the client section in System.ServiceModel from web.config file
var clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
//get the endpointsCollection from childSection
var endpointCollection =
clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection;
foreach (var serviceInterface in servicesWithWcfInterfaces)
{
//get the childEndpoint name from web.config file
var endpointName = GetClientEndpointName(endpointCollection, serviceInterface);
//register services which are declared in web.config file only.
if (string.IsNullOrEmpty(endpointName)) continue;
// attribute is either on the service class or the interface
var attribute =
(ServiceContractAttribute)
(Attribute.GetCustomAttribute(serviceInterface, typeof (ServiceContractAttribute)));
if (attribute != null)
{
WcfClientModelBase model = null;
// handle duplex differently
if (attribute.CallbackContract != null)
{
model = new DuplexClientModel
{
Endpoint =
WcfEndpoint.ForContract(serviceInterface).FromConfiguration(endpointName)
}.Callback(container.Resolve(attribute.CallbackContract));
registrations.Add(WcfClient.ForChannels(model).Configure(c => c.LifestyleSingleton()));
}
else
{
//regular attributes
model = new DefaultClientModel
{
Endpoint = WcfEndpoint.ForContract(serviceInterface).FromConfiguration(endpointName)
};
registrations.Add(WcfClient.ForChannels(model).Configure(c => c.LifestyleTransient()));
}
}
}
return container.Register(registrations.ToArray());
}
Am hosting only one duplex service and the below are the servicecontracts -
[ServiceContract(CallbackContract = typeof(INotificationCallback))]
public interface INotificationService
{
[OperationContract(IsOneWay = false)]
void Subscribe(Guid subscriptionId, string userName, string[] eventNames);
[OperationContract(IsOneWay = true)]
void EndSubscribe(Guid subscriptionId);
}
[ServiceContract]
public interface INotificationCallback
{
[OperationContract(IsOneWay = true)]
void ReceiveNotification(NotificationResultDto notificationResult);
}
[DataContract]
public class NotificationResultDto
{
[DataMember]
public string UserName { get; set; }
[DataMember]
public string NotificationMessage { get; set; }
}
When I try to resolve the duplex service using the below statement.
var temp = _container.Resolve();
I get error -
WcfClientActivator: could not proxy component c2a216c2-af61-4cb2-83ba-e4d9a5cc4e68
with inner exception - The Address property on ChannelFactory.Endpoint was null. The ChannelFactory's Endpoint must have a valid Address specified.
in the web.config file under client section -
<endpoint address="net.tcp://localhost:9877/NotificationService" binding="netTcpBinding"
bindingConfiguration="netTcpBindingConfiguration" contract="ServiceContracts.INotificationService"
name="INotificationService_Endpoint" />
After few hours of struggling, I found a work around for this problem.
I think this could a bug in Castle Windsor, while creating DuplexClientModel, endpoint cannot be created using "FromConfiguration". It fails while resolving during runtime. However samething works fine with "DefaultClientModel".
My workaround was to read the config file and get the address, binding and contract details and use them to create Endpoint in code.
model = new DuplexClientModel
{
//Endpoint = WcfEndpoint.ForContract(serviceInterface).FromConfiguration(endpointName)
//FromConfiguration method is failing for some reason,could be b.u.g in castle,
//so had to do this workaround by reading the web.config file and creating the Endpoint
//from there manually.
Endpoint = WcfEndpoint.ForContract(serviceInterface)
.BoundTo(CreateBindings(clientEndpoint.Binding))
.At(clientEndpoint.Address)
}.Callback(container.Resolve(attribute.CallbackContract));

WCF and client code

I am beginner in WCF. I have two simple services: adding client first and last name, and returning number of clients. When I test the service to the client whenever a customer adds, the number is not increasing is always one. Here code:
class ClientService : IClientService
{
List<Client> list = new List<Client>();
public bool SubmitClient(Client client)
{
list.Add(client);
}
public int IClientService.GetClient()
{
return list.Count;
}
}
Client code:
protected void Button1_Click(object sender, EventArgs e)
{
ClientServiceClient channel = new ClientServiceClient();
Client cl = new Client();
cl.FirstName = txtFName.Text;
cl.LastName = txtLName.Text;
channel.SubmitClient(cl);
labbel1.Text=string.Format("Number of clients: {0}"),channel.GetClient());
}
You need to provide the InstanceContextMode value in the ServiceBehavior attribute. The prolbem is that your list is a class level variable, you need to configure the WCF service as Single instance mode.
PerSession(default): a new InstanceContext object is created for each session.
PerCall: a new InstanceContext object is created prior to and recycled subsequent to each call.
Single: only one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls.
Here is a good link on the subject of control the WCF instances/sessions:
http://www.codeproject.com/Articles/86007/3-ways-to-do-WCF-instance-management-Per-call-Per

How to send an PUT request to a WCF 4.0 REST service that is protected with OAuth 2.0 and DotNetOpenAuth

The question:
Are there any means (perhaps a workaround) to support the PUT and DELETE HTTP verbs with a WCF 4.0 REST service secured using OAuth 2.0 and DotNetOpenAuth 4? I'm not using the WCF Web API or WCF Starter Kit.
What I have done so far:
Consider the following WCF 4.0 REST Service:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService
{
[WebGet(UriTemplate = "")]
public List<Resource> GetCollection()
{
/* some implementation */
}
[WebInvoke(UriTemplate = "", Method = "POST")]
public Resource Create(Resource instance)
{
/* some implementation */
}
[WebGet(UriTemplate = "{id}")]
public Resource Get(string id)
{
/* some implementation */
}
[WebInvoke(UriTemplate = "{id}", Method = "PUT")]
public Resource Update(string id, Resource instance)
{
/* some implementation */
}
[WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
public void Delete(string id)
{
/* some implementation */
}
}
OAuth 2.0 is used for controlling access to the service. It is achieved with a custom ServiceAuthorizationManager implementation using DotNetOpenAuth to do all the work. The implementation is almost identical to that provided by the DotNetOpenAuth 4.0 samples.
For POST and GET requests it works great. However, the requests fail for the PUT and DELETE requests with the error message:
'AccessProtectedResourceRequest' messages cannot be received with HTTP verb 'PutRequest'
The client code looks as similar to:
public class ResourceClient
{
public Resource UpdateResource(Resource resource)
{
var uri = new Uri(new Uri("http://api.example.com/"), Resource.Format("resources/{0}", resource.Id));
var serializer = new DataContractSerializer(typeof(Resource));
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "PUT";
request.ContentType = "application/xml";
ClientBase.AuthorizeRequest(request, Authorization.AccessToken);
using (var requestStream = request.GetRequestStream())
{
serializer.WriteObject(requestStream, resource);
}
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
Debug.Assert(responseStream != null, "responseStream != null");
return (Resource)serializer.ReadObject(responseStream);
}
}
}
}
From the answer of another question it seems that this is may be a deficiency in DotNetOpenAuth. Looking at the OAuth 2.0 spec, I don't see anything prohibiting the use of the PUT or DELETE http verbs when accessing resources. That said, I'm not an expert in OAuth 2.0, nor do I know it by heart.
So I thought I would be "clever" and use the "X-HTTP-Method-Override" to specify PUT and then POST the request to the resource server. I implemented the behaviour as explained in this sample. Interestingly enough the custom behavior was called before my ServiceAuthorizationManager implementation, resulting in the exact same error.
An alternative would be to address the issue within DotNetOpenAuth itself, but I have no idea where to start.
I'm running out of ideas and I'd appreciate any suggestions.
This is tracked by Issue #62. You can glance at the fix for it if you want to port it to your version without waiting for the next release.

WCF WebApi's Self-Hosting not accepting PUT verbs

I have put together a HTTP driven API using the WCF WebAPI that uses the PUT verb. When hosted inside of an MVC3 project that is hosted upon IIS Express, everything works as designed.
However, when I unit-test I'm occasionally wanting to test the transport aspects rather than just against my own resources. My unit-tests fail with a 405 - MethodNotAllowed. Again, exactly the same service hosted in IIS works (where I enabled the PUT and DELETE verbs in the configuration file).
How can I get the 'self-hosted' service, as used in my testing, to accept these verbs too?
The almost identical 'get' tests work, so I'm not expecting the concept of the following to be at fault.. hopefully...
[Test]
public void PutNewMachine()
{
// Create new record to add
var machine = new Machine
{
ID = 1,
Name = "One",
Description = "Machine #1",
Location = 1
};
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(
HttpMethod.Put,
HOST + "/1"))
{
request.Content = new ObjectContent<Machine>(machine);
using (var response = client.Send(request))
{
Assert.AreEqual(
HttpStatusCode.Created,
response.StatusCode,
"New record put should have been acknowledged "
+ "with a status code of 'Created'");
}
}
}
}
In the setup to the test, I'm preparing the end-points using the following Autofac code (and again this works for the 'Get'):
var builder = new ContainerBuilder();
builder
.Register(c => new FakeDatabase())
.As<IDatabase>()
.SingleInstance();
builder
.Register(c => new GenericRepository<Machine>(c.Resolve<IDatabase>()))
.As<IResourceRepository<Machine>>();
builder
.Register(c => new MachineService(c.Resolve<IResourceRepository<Machine>>()))
.As<MachineService>();
Container = builder.Build();
Scope = Container.BeginLifetimeScope();
host = new HttpServiceHost(typeof(MachineService), HOST);
host.AddDependencyInjectionBehavior<MachineService>(Container);
host.Open();
My service is defined in the following interface:
[ServiceContract]
public interface IResourceService<in TKey, TResource>
{
[WebGet(UriTemplate = "{key}")]
TResource Get(TKey key);
[WebInvoke(Method = "PUT", UriTemplate = "{key}")]
TResource Put(TKey key, TResource resource);
[WebInvoke(Method = "POST")]
TResource Post(TResource resource);
[WebInvoke(Method = "DELETE", UriTemplate = "{key}")]
void Delete(TKey key);
}
So, for example, if I have a MachineService, it implements the interface (both class MachineService : IResourceService<string, Machine> and ... : IResourceService<int, Machine> have been trialled - Get = OK, Put = Nothing.
EDIT: I seem to be bouncing between InternalServerError and MethodNotAllowed errors - only when using the self-hosting. I have ensured that I, as a user, have rights to open the port (Win7 + non-admin) but the results of that plus my choice of ports seems predicable functional for Get. "Post" seems to be having similar issues! :-(
EDIT2: Interface has now changed to which works!
[ServiceContract]
public interface IResourceService<in TKey, TResource>
{
[WebGet(UriTemplate = "{key}")]
TResource Get(TKey key);
[WebInvoke(Method = "PUT", UriTemplate = "{key}")]
TResource Put(HttpRequestMessage<TResource> resourceRequest, TKey key);
[WebInvoke(Method = "POST", UriTemplate = "{key}")]
TResource Post(HttpRequestMessage<TResource> resourceRequest, TKey key);
[WebInvoke(Method = "DELETE", UriTemplate = "{key}")]
void Delete(TKey key);
}
Doing PUT or POST works for me when I change the method signature to accept a HttpRequestMessage request instead of T itself.

Bad Request 400 error when trying to do a HTTP POST

I have written a WCF REST Service as follows
namespace UserService
{
// TODO: Modify the service behavior settings (instancing, concurrency etc) based on the service's requirements. Use ConcurrencyMode.Multiple if your service implementation
// is thread-safe.
// TODO: Please set IncludeExceptionDetailInFaults to false in production environments
[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceContract]
public class Service
{
UserManager userManager = new UserManager();
[OperationContract]
[WebGet(UriTemplate = "{userName}")]
[WebHelp(Comment = "Gets an user object given the username")]
public User GetUser(string userName)
{
return userManager.Read(userName);
}
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "", RequestFormat=WebMessageFormat.Xml, ResponseFormat=WebMessageFormat.Xml, BodyStyle=WebMessageBodyStyle.Bare)]
[WebHelp(Comment = "Creates an User")]
public void CreateUser(User user)
{
userManager.Create(user);
}
}
}
I am accessing this from my ASP.NET application as follows.
HttpClient client = new HttpClient();
HttpContent content = null;
DiscussionForum.Library.User user = new User();
user.UserEmailAddress = emailAddressTextBox.Text;
user.UserName = userNameTextBox.Text;
user.UserPassword = passwordTextBox.Text;
content = HttpContentExtensions.CreateXmlSerializable<DiscussionForum.Library.User>(user);
content.LoadIntoBuffer();
HttpResponseMessage response = client.Post(new Uri("http://localhost/UserService/Service.svc"),"application/xml", content);
Response.Write(response.StatusCode.ToString());
I am getting a Badrequest 400 in the status code on the client side.
Am I missing something?
Well, one thing I immediately see is that your service uses the DataContractSerializer and your client uses the XmlSerializer, and so the XML representations of the "User" type probably aren't the same.
Either use the [XmlSerializerFormat] attribute on the service, or use HttpContentExtensions.CreateDataContract on the client (but not both of course :)
But I'm not 100% sure that this is your problem (or the only problem)... If it's not, reply here and I can help with some debugging tips.