Asp.Net MVC and WCF - good idea to just call instance methods directly? - asp.net-mvc

I'm beginning development of a new Asp.Net MVC app. One of the requirements is to expose our API using a webservice - we're going to use WCF for this. (We have a third party making an iPhone app that will consume this service)
I'm writing the Asp.Net MVC application that will also consume the WPF API.
I'm thinking that because my MVC app will be installed on the same instance of IIS that the WCF app is on, and it will also be part of the same Visual Studio project that I could just call the service methods directly - instead of making calls through the web service.
For example in my controller class I could just do something like
WcfService.SomeClass someServiceClass = new WfcService.SomeClass();
var stuff = someServiceClass.GetSomeStuff()
Is this possible? If so is it wise?

Is this possible?
Yes. You could either import the assembly containing your WCF service contract and implementation into the ASP.NET MVC application and directly call it from there.
If so is it wise?
Yes, you will gain performance this way as you will be short-circuiting the whole serialization/deserialization and network call process.
So if the two are hosted inside the same ASP.NET application you could do it.

Is this possible?
Sure. Darin is right about this.
Is It wise?
That depends ! Your WCF service can be implemented in multiple ways, for example you can (and maybe should) implement concurrency and instance management other that "per call" - which will work only when service is hosted in WCF capable hosting enviroment and called properly. If you start using it this way
WcfService.SomeClass someServiceClass = new WfcService.SomeClass();
var stuff = someServiceClass.GetSomeStuff()
you are giving up on some very good features WCF offers you to improve scalability and performance. Give it a thought, if it the "serialization-deserialization" overhead is worthy of it. It very well can be !

Related

Approach to create service oriented application for MVC application using Web Api

I am planning to create a service oriented application. For creating service I will be using WebApi and for web application MVC.
I have thought of two approach for the same kindly let me know which one better.
Create one business logic shared across MVC and WebApi and dont consume WebApi in MVC application. Just business logic will be shared.
Consume API in MVC application from WebApi project using HttpClient.
The question was a bit unclear but now, after comments it makes more sense.
I would go with option 1.
The first reason is that you can avoid a lot of traffic to the API if you can simply use the layer and don't bother with any APi calls at all.
The second reason, it will be a little quicker since it doesn't need to wait for the API responses.
Less pressure on the API means the mobile app has more resources to play with.
You can basically build your own Nuget packages, use them in both your Web and Api layers and you don't have to repeat the code.

ASP.NET MVC and legacyCasModel=true

I am creating a fresh ASP.NET MVC4 website using a proprietary API that tells me I need to include <trust level="Full" legacyCasModel="true"/> in my Web.config file. When I do this, however, the application exceptions out with the message "Dynamic operations can only be performed in homogenous AppDomain" I have poked around a bit and it seems that this has to do with some dynamic calls not being allowed.
My question is: Does that mean I have to abandon MVC altogether and make my site with Web Forms?
Perhaps you can move the code which calls the API to separate DLL, make your calls there and expose that DLL as a web service (or .NET Remoting or whatever works). Then, from your MVC web-app, call your new web service in order to reach the proprietary API.
Basically, wrap the API calls in a (local) web service call.
This approach would allow you to use MVC instead of WebForms. It does add some complexity and overhead, but I think the pros (ability to use MVC) outweigh the cons.

Web Services in ASP.NET MVC 4 Application

I've a web site developed using MVC 4 ASP.net application. I'm new to .net platform & I want to add web service which would return me operating system name of users device based on certain input.
Assuming I've logic to capture OS information using inputted data, how do I go forward in building this web service?
Do I need to have a complete separate solution file which will have a web service or in existing MVC 4 asp.net application itself, should I create a new project which would be of type "WCF Service Application"? Again I don't know much about WCF service either, if I use it, how would the URL be accessible, etc?
Can anyone give me some insights?
Note: I've also a separate REST web service which is a completely separate solution with separate projects but deployed on same IIS.
Thanks in Advance!
You don't need to create a WebAPI project just for what you described (i'm assuming one or a few end points).
Simply use MVC controllers that return JSON for example, this way you deal with a single framework.
Reasons to move to Web API is if you need support for CORS, need content negotiation for results etc. From what you are describing it's completely fine to stay with MVC.

ASP.NET MVC Enterprise DDD Architecture and WCF layer

I've desgined my ASP.NET MVC application using the Domain Driven Design, and I got the following projects:
MyApp.Core - the app core, contains the domain models etc.
MyApp.Infrastructure - the app main infrastrucutre, contains implementation for the domain model storing (repos etc.) using EF.
MyApp.Web.Core - domain models, services declaration (interfaces) and such only for web (e.g. IFormAuthenticationTicketSupplier, IOAuthAuthenticationProvider etc.)
MyApp.Web.Infrastructure - web implementation
MyApp.Web.UI - ASP.NET MVC standard application.
This application should be used by enterprise with multiple servers, etc. Currently, the application calls a service in the infrastructure layer at the controllers, which uses Repositories and EF. I can connect to the DB server using the connection string.
When digging about this topic at Google, I've read that some approches taken when creating an enterprise application are create an Application server and Web server. In the application server - storing a WCF service, and in the web server just calling it.
I'd like to know if I should do so (if creating a WCF service is the right and required approch when dealing with enterprises):
- Why should someone not just use the Services in the controllers and instead use an API?
- In case I'm using an API, it won't slow down the response? since even if the computers are on the same network, I still open an HTTP request.
- If I should use WCF, or ASP.NET WebAPI?
Thanks for any feedback and help!
First, regarding your projects, is there a need to split up MyApp.Web.Core, MyApp.Web.Infrastructure and MyApp.Web.UI? Sure they may be separate responsibilities, but sometimes dependency hygiene trumps encapsulation. You can always leave them in separate folders and namespaces. I wouldn't extract something into a separate project unless I needed to reference that as a library from elsewhere.
As far as the application service, that also depends on your needs. If the only place that would call that application service is the ASP.NET MVC app, then there isn't much of a need to extract an application service. There are some benefits however. One is that you don't have to worry about all of the dependencies required for a service - you just references it via Url. And of course you have the ability to call the service from places other than the controller, although the MVC controller can act as a pure HTTP service as well. You also have the ability to deploy updates to a specific service without releasing the MVC app. But you do have the burden of maintaining a separate service. If you do go that route, go with the WebAPI, WCF is just too much abstraction.

asp.net mvc3 - calling controller actions from external app

I have an asp.net mvc3 application built with RavenDB and I want to be able to access the data via an external HTML5 mobile app. I'm thinking of exposing methods via WCF or via MVC controller action methods? Which option is best?
Ok, I have faced similar situation a while ago. This the way I have handled it, I have directly exposed Controller urls to the mobile application clients. Bascially it will help you in reducing the burden of maintaining two code bases and helps you to reuse existing functionality. Even if you go with WCF you need to expose with REST to make HTML5 client developer life easy.
This is the why microsoft released ASP.NET MVC 4 Web Apis to avoid confusion among developers which way to go in these scenarios. So that your services are device agnostic and easily testable.
Since you've already built the app in MVC3, I'd recommend a JsonResult action on an MVC Controller: http://www.asp.net/ajaxlibrary/jquery_json_data_from_controller.ashx

Resources