WCF service hangs when calling method, however method is working - sql

Hello I'm having issue with my wcf service. I'm trying to call method which returns list of type object. I'm also using entity framework.
public IList<Product> GetAllProducts()
{
using (var db = new AuctionContext())
{
return db.Products.ToList();
}
}
</service>
<service name="AuctionSystem.WcfService.ProductService">
<endpoint address="" binding="wsDualHttpBinding" contract="AuctionSystem.WcfService.Contracts.IProductService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:9993/Design_Time_Addresses/AuctionSystem.WcfService/ProductService/" />
</baseAddresses>
</host>
</service>
And contract :
[OperationContract]
IList<Product> GetAllProducts();
The method itself is working, but when I try to invoke this method on my wcf service UI it got stuck at "invoking service" I'm using wsdualhttpbinding.
Any ideas please?
EDIT: I realized in Product object I have virtual List, why is this List causing wcf to hang?

For all wondering why It was caused because of circular dependency.

Related

Not able to call a wcf service from client

Getting an error "System.InvalidOperationException: 'Attempted to get contract type for ITC2DataService,
but that type is not a ServiceContract, nor does it inherit a ServiceContract.".
Note : I have already decorated the interface "ITC2DataService" with "ServiceContract" attribute. But still error occurs.
In addition to adding ServiceContract to ITC2DataService, the service class also inherits derived contracts, such as the following :
public class Service1 : ITC2DataService
{
...
}
The corresponding endpoint binding in the Web.config file needs to specify the correct contract (like the endpoint address "ws" below).
<services>
<service name="WcfService.Service1" behaviorConfiguration="WcfService.Service1Behavior">
<!-- ITestCallback needs to be the contract specified -->
<endpoint address="ws" binding="wsHttpBinding" contract="WcfService.ITestCallback">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>

how to bind endpoints?

although there are many posts about the same issue I still haven't figured out how to solve the problem regarding endpoints.
In the solution there are several projects and after reading about similar problems I edited the app.config file of the StartUp Project the following way:
<system.serviceModel>
<services>
...
<service name="LiveGames.Engine.LoginService">
<endpoint address="" name="ILoginService" binding="wsHttpBinding" contract="LiveGames.Entities.Interfaces.ILoginService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/LiveGames.Engine/LoginService/" />
</baseAddresses>
</host>
</service>
</services>
<client>
<endpoint address="http://localhost:8732/LiveGames.Engine/LoginService/"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ILoginService" contract="LiveGames.Entities.Interfaces.ILoginService"
name="ILoginService" kind="" endpointConfiguration="">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
I created the Proxy class with the following methods:
public ChannelFactory<T> GetFactory<T>(string serviceName, out T channel)
{
ChannelFactory<T> factory = new ChannelFactory<T>(serviceName);
channel = factory.CreateChannel();
return factory;
}
public JSONUserLogin Login(string username, string password)
{
JSONUserLogin retval = new JSONUserLogin();
ILoginService sec = null;
ChannelFactory<ILoginService> factory = null;
try
{
using (factory = GetFactory<ILoginService>("ILoginService", out sec))
{
retval = sec.Login(username, password);
}
return retval;
}
catch (Exception ex)
{
return new JSONUserLogin();
}
finally
{
if (sec != null)
((IChannel)sec).Close();
if (factory != null)
factory.Close();
}
}
When serviceName="ILoginService" and the execution hits the line ChannelFactory<T> factory = new ChannelFactory<T>(serviceName);
it throws an exception:
System.InvalidOperationException: Could not find endpoint element with name 'ILoginService' and contract 'LiveGames.Entities.Interfaces.ILoginService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.
Does anyone know what could be wrong here and how to fix the problem?

WCF and ef 4.0 performance is very bad .... Site works very slow

we have a project in which we have used WCF because we have multiple sites which pull up the same data .... We have used HTTPBinding in WCF and EF 4.0 to interact with the database .. When it was moved to production environment we found that the site was very slow .... Do you know any way how we could increase the performance drastically ... EF is causing a lot performance issues ... Please suggest next steps
Service Contract
[ServiceContract]
public interface ICommonService
{
[OperationContract]
LoginDTO AuthenticateUser(string userName, string password, int ownerId);
}
<service name="MyService.Services.CommonService">
<endpoint binding="basicHttpBinding" bindingConfiguration=""
name="CommonServiceEndpoint" contract="MyService.Services.Contracts.ICommonService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/MyService.Services/ICommonService/" />
</baseAddresses>
</host>
</service>
We have introduced indexing but it doesn't increase performance so much
using (MyService.DataAccess.MyService_RedesignEntities context = new MyService_RedesignEntities())
{
context.ContextOptions.ProxyCreationEnabled = false;
context.ContextOptions.LazyLoadingEnabled = false;
ObjectParameter StrOutput = new ObjectParameter("chvnOutputMesage", SqlDbType.NVarChar);
objResult = context.spAuthenticateUser(ownerId, userName, encryptedPassword).FirstOrDefault();
return objResult;
}

wcf service hosted under IIS address

I have a WCF service hosted under IIS.
I have the following configuration:
<services>
<service name="BillboardServices.LoginService" behaviorConfiguration="LoginServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://myip/LoginService/" />
</baseAddresses>
</host>
<endpoint address="" name="LoginService" binding="basicHttpBinding" contract="BillboardServices.ILoginService" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
If I enter http://myip/LoginService/, I get a 404.
If I enter http://myip/Service1.svc, I get the service metadata.
What changes to the configuration do I need in order for the service to be accessible through the nice url?
Thank you.
In order to have and extensionless service, you need to use WCF 4 and init the routing engine in the global.asax file like so:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
// Edit the base address of Service1 by replacing the "Service1" string below
RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
}

How use injection in wcf

I have two classes implementing a contract at a service which are consumed from a factory on the client like shown below.
[ServiceContract]
public interface MyInterface {
void DoSomething()
}
public class A : MyInterface {
public void DoSomething(){
"Hi I'm A"
}
}
public class B : MyInterface {
public void DoSomething(){
"Hi I'm B"
}
}
public class MyFactory <TMyInterface> {
void DoSomething(){
TMyInterface.DoSomething()
}
}
The client must remain the same. My question is how can I choose at the server side which implementation of MyInterface to use, by passing the type parameter using .config file in WCF
I read other post but I don't understand yet :(
It is possible to do it, and there are a few ways to do it.
One possibility is to create a "routing" service which will contain the "public" address which the client always talks to. This routing service can then, based on some configuration, redirect the call to the appropriate "real" service.
Another way is to actually have a process which starts both services, but their addresses are defined in config. If you use the same binding and the same contract (which is the case), then you can "flip-flop" the service address when you want to change the service which will receive the calls from the client. For example, this configuration directs the requests to the endpoint at "http://machine-name:8000/Service" to service A. Notice that, since you define service hosts for both services, you actually need to have a base address for service B as well - in this case I used named pipes, which cannot be accessed via different machines.
<system.serviceModel>
<services>
<service name="A">
<host>
<baseAddresses>
<add baseAddress="http://machine-name:8000/Service"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="MyInterface" />
</service>
<service name="B">
<host>
<baseAddresses>
<add baseAddress="net.pipe://localhost/ServiceBackup"/>
</baseAddresses>
</host>
<endpoint address="" binding="netNamedPipeBinding" contract="MyInterface" />
</service>
</services>
</system.serviceModel>
when you want to change the address for B, you'd then swap the addresses.
<system.serviceModel>
<services>
<service name="A">
<host>
<baseAddresses>
<add baseAddress="net.pipe://localhost/ServiceBackup"/>
</baseAddresses>
</host>
<endpoint address="" binding="netNamedPipeBinding" contract="MyInterface" />
</service>
<service name="B">
<host>
<baseAddresses>
<add baseAddress="http://machine-name:8000/Service"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="MyInterface" />
</service>
</services>
</system.serviceModel>
The hosting program would look like this:
public static void HostServices()
{
ServiceHost hostA = new ServiceHost(typeof(A));
ServiceHost hostB = new ServiceHost(typeof(B));
hostA.Open();
hostB.Open();
Console.WriteLine("Press ENTER to close");
Console.ReadLine();
hostA.Close();
hostB.Close();
}
Now, if your services are hosted in IIS (webhost), then it's a little harder. Since the "normal" activation requires a .svc file to be part of the endpoint address, and each .svc file is associated with a single class, the address for A would be something like http://machine-name/services/a.svc while the address for B would be something like http://machine-name/services/b.svc. So what you'd need to do in this case is to create a custom ServiceHostFactory, and use the ASP.NET Routes integration to create a .svc-less URL for your service. Then you'd use something similar to the previous example to decide which service will be activated.