I'm using a service locator to hold concrete instances of classes, which is used in a WCF service. Rather than passing the ServiceLocator around I decided to make it static so I could access it from every class. The service locator is populated when the WCF service kicks off and depending on some parameters, it can get populated with different concrete implementations.
My problem is that if a call is made to the WCF service before another call has finished, then (I think) the ServiceLocator will be populated with the incorrect implementations.
Is there any way to make the service locator available to classes and not have it re-used by different threads or calls to the WCF service?
You provide few details about your Service Locator and the undelying implementation. From what I read I understand that it's your own.
My personnal advice is that you should not use a Service Locator in a case of WCF service because WCF exposes some interface that enable dependency injection.
You could read more about the anti pattern Service Locator here : http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx
You should not implement your service locator because there are many OSS project around dependency injection you can use and that do the job the right way.
Here is the correct implementation of Dependency Injection using StructureMap with WCF services : http://lostechies.com/jimmybogard/2008/07/30/integrating-structuremap-with-wcf/.
Related
Is it possible to create liferay service builder without any configuring any database tables in service.xml file.
Actually purpose here is to create a service layer using liferay service builder. And there is no database interaction directly in this service layer.
Yes, and it's quite simple. While you still need an entity (which provides the name for your service) you can leave this entity definition empty.
This will create the service (local or remote, as configured in the entity) but no model, no persistence and no database table.
One of the situations where this comes in really handy is when you want to add another method to an existing service (which you can't) - you just create a new service with your custom methods and delegate to the original service.
I agree with #Olaf Kock answare in which say that it is possible have an empty model with service builder. Furthermore have an empty entity you can benefit of have the same transactional context of your portal and benefit of cluster managing and benefit of a complete integration with liferay portal.
If you have the same transactional enviroment of the portal you can image of create a service that agregate native liferay service and you get the assurance that the transactional context is the same of the portal.
I hop that this reflection can add value.
Its highly recommended that If you're creating Service.xml then at least one entity should be there. Otherwise no need to add that configuration.
Able to create service builder without real entities.
As provided in the link it is possible to create service builder without entities.
Also discussed more in detail in this forum
Hope it helps some one. Thanks
I have a dashboard service that relies on other services to retrieve data. So in order to reuse existing services I'm calling ResolveService for each service I'm reusing.
My question is if it would be better for me to extract the logic from these services rather than resolving the service? In particular are there performance impacts if I were keep calling ResolveService?
It can be cleaner to extract the logic in shared dependencies, which allows for more finer-grained usage (i.e you call just what you need instead of the entire Service). But if you need to use the entire Service response than calling ResolveService<Service> is fine.
The performance impact is no different since it's essentially just resolving an a Service class from the IOC and executing it.
Service Fabric was just announced at the build conference. I was reading the scarce documentation about it and I have a question.
I'm evaluating Service Fabric for hosting CRUD like microservices that are at the moment built in ASP.NET WebApi.
Is Service Fabric geared towards hosting small pieces of functionality that receive data, process it and return the result, rather than hosting CRUD WebApi types of application?
Service Fabric enables the creation of both stateless and stateful microservices.
As the name suggests, any state maintained by an an instance of a stateless service will be lost if the node goes down. A new, fresh instance will simply be spun up elsewhere in the cluster.
Stateful services offer the ability to persist state without relying on an external store. Any data stored in a Reliable Collection will be automatically replicated across multiple nodes in the cluster, ensuring that the state is resilient to failures.
A common pattern is to use a stateless service as the client-facing gateway to the application and then have that service direct traffic to the app's partitioned stateful services. This hides the work of resolving partitions from clients, allowing them to to target one logical endpoint with all requests.
Take a look at the WordCount sample for an example of how this works. The WordCount.WebService stateless service acts as the front end to the application. It simply resolves the partition based on the incoming request and then sends it on. The WordCount.Service stateful service (partitioned based on the first letter of the word) immediately puts those incoming requests in a ReliableQueue and then processes them in the background, storing the results in a ReliableDictionary.
For more details, see the Reliable Services Overview.
Note: for now, the best way to expose WebAPI endpoints to clients is to self-host an OWIN server in the stateless service. ASP.NET 5 projects will soon be supported as well.
This video answers my own question: http://channel9.msdn.com/Events/Build/2015/2-704. In summary, we should use Stateless Services to host ASP.NET based sites or API's which persist data to external data stores.
If you don't have state (or have it externally), Stateless Service is the way to start.
Answer to the original question is "both". Basically, anything that have main() function (with couple of more extended contract methods to talk to Service Fabric) can be a service in Service Fabric world.
Which of the following util is suitable for creating an organization using Liferay API.
i) OrganizationUtil
ii) OrganizationServiceUtil
iii) OrganizationLocalServiceUtil
Basically, I want to know the difference between these three.
i) OrganizationUtil: com.liferay.portal.service.persistence.OrganizationUtil
The classes from the persistence layer directly talk to the Database and hence are good to be used from the service layer and also if you care about transaction.
Following are the words from the documentation:
The persistence utility for the organization service. This utility wraps OrganizationPersistenceImpl and provides direct access to the database for CRUD operations. This utility should only be used by the service layer, as it must operate within a transaction. Never access this utility in a JSP, controller, model, or other front-end class.
ii) OrganizationServiceUtil: com.liferay.portal.service.OrganizationServiceUtil
It can be called from any layer as such. This class also does permission checks (based on Permissions given in Liferay) which may be useful in some cases. This can also be used through web-service.
Well lets see what liferay's documentation has to say:
The utility for the organization remote service. This utility wraps com.liferay.portal.service.impl.OrganizationServiceImpl and is the primary access point for service operations in application layer code running on a remote server.
This is a remote service. Methods of this service are expected to have security checks based on the propagated JAAS credentials because this service can be accessed remotely.
iii) OrganizationLocalServiceUtil: com.liferay.portal.service.OrganizationLocalServiceUtil
This can also be used if you don't want any permission checks. The OrganizationServiceUtil ultimately makes a call to the localService layer.
Liferay's Documentation:
The utility for the organization local service. This utility wraps com.liferay.portal.service.impl.OrganizationLocalServiceImpl and is the primary access point for service operations in application layer code running on the local server.
This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
Conclusion
Use OrganizationUtil if you care about transaction i.e. have to update multiple tables in a transaction then use this.
Use OrganizationServiceUtil if you creating Organization outside liferay or if you need permission checks & you don't care about transaction (i.e. transaction with your custom code)
Use OrganizationLocalServiceUtil if you are not using a web-service and you don't care about transaction or permissions.
Hope this gives you a fair idea. Let me know if it is still unclear.
Want to get a clear understanding of how WCF works.
Lets say a WCF service has exposed a function A.
Now the client creates 5 threads, and in each one of them calls the function A with different parameters.
I assume this should happen - a new instance of function A would get created for every thread call to that function. Could some one confirm this.
I have written a POC which is not doing this, its giving inconsistent results.
This depends on your service configuration via the ServiceBehavior attribute on the class implementing your service contract:
[ServiceBehavior(
InstanceContextMode = InstanceContextMode.PerSession,
ConcurrencyMode = ConcurrencyMode.Multiple)]
With the parameter InstanceContextMode you tell WCF how you want to host your service:
Single: One instance of your service class will be created which receives all service calls
PerSession: For each connecting client a new instance will be created
PerCall (default): For each call of every client a new instance will be created
The next thing is synchronisation, when one host object receives parallel operation calls. You can control the behavior with the ConcurrencyMode parameter:
Single (Default): WCF serialises all operations, so your service instance is executing exactly one or no operation call at a time.
Reentrant: WCF delegates all operation calls to your service instance directly, but synchronises calls to another WCF service inside a service operation (rarely used i think).
Multiple: WCF delegates all operation calls to your service instances directly without synchronization. You have to worry about synchronisation yourself.
The default is for a new instance of your wcf service to be created per call, this is documented e.g. here
There is no such thing like a new instance of function. It'd be rather instance of class.
You can configure how your Service should behave by changing proper meta attribute.
You can make your service implementation instantiated per call. It can also work as singleton (one and only service instance for all calls).
Here you can find information on creating singleton WCF service
And here there is a lot more about WCF services