.Net WebAPI 访问其他项目层操作
2021-06-21 07:06
标签:custom tab eric ict cto readonly array eof oda 1.首先,配置一个公用的WEBAPI服务接口: 2.在引用的项目层里添加JasonContainer类(即引用),用来访问对应的类 ,规则是以BL开头及以Service结尾的.cs文件,通过反射生产对应的类实例 3.在引用的项目层里添加AssemblyProvider.cs 4.WebAPIConig.cs 前台代码可以配置一个公用的访问层来调用webapi (以AngularJS为例) .Net WebAPI 访问其他项目层操作 标签:custom tab eric ict cto readonly array eof oda 原文地址:https://www.cnblogs.com/Aaron-Lee/p/10244206.htmlusing System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAPIService.Utility; ---引用的层
using Exceptions; ---引用的层
namespace Syngenta.FWA.WebUI.Controllers
{
[RoutePrefix("api/AppService")]
public class AppServiceController : ApiController
{
// GET api/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace WebAPIService
{
public class JsonContainer
{
const string BusinessNamespace = "BusinessLayer.";
const string ServiceNamespace = "WebAPIService.Implements.";
const string AdapterNamespace = "WebAPIService.Adapter.";
public string Class { get; set; }
public string Action { get; set; }
public Dictionarystring, object> Parameters { get; set; }
public object GetObject()
{
if (this.Class.EndsWith("service", StringComparison.OrdinalIgnoreCase))
{
var t = Type.GetType(ServiceNamespace + Class);
if (t == null) t = Type.GetType(ServiceNamespace + "BL" + Class);
object obj = Activator.CreateInstance(t);
var method = FindMethod(t);
return method.Invoke(obj, GetParameters(method));
}
else
{
if (!Class.StartsWith("BL", StringComparison.OrdinalIgnoreCase)) Class = "BL" + Class;
Assembly assembly = AssemblyProvider.Provider.GetAssembly(AssemblyProvider.BusinessAssembly);
var t = assembly.GetType(BusinessNamespace + Class);
object bl = Activator.CreateInstance(t); //BL class instance
var method = FindMethod(t);
//Find if there‘s adpater
var tAdapter = Assembly.GetCallingAssembly().GetType(AdapterNamespace + Class + "Adapter");
if (tAdapter == null)
{
return CallMethod(bl, method);
}
else
{
object objAdapter = Activator.CreateInstance(tAdapter);
MethodInfo methodAdapter = tAdapter.GetMethod(Action + "Convert");
if (methodAdapter == null) //No such Apdater method, invoke BL method directly
return CallMethod(bl, method);
return methodAdapter.Invoke(objAdapter, new object[] { Parameters, bl, method });
}
}
}
private MethodInfo FindMethod(Type t)
{
var members = t.GetMember(Action, MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance);
if (members.Length == 1) return members.Single() as MethodInfo;
else if (members.Length > 1)
{
var methods = members.Cast
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace WebAPIService
{
public class AssemblyProvider
{
static readonly AssemblyProvider instance = new AssemblyProvider();
public const string BusinessAssembly = "BusinessLayer";
static readonly Assembly businessAssembly = Assembly.Load(BusinessAssembly);
static AssemblyProvider()
{
}
AssemblyProvider()
{
}
public Assembly GetAssembly(string assembleName)
{
if (assembleName == BusinessAssembly)
return businessAssembly;
return null;
}
public static AssemblyProvider Provider
{
get
{
return instance;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace Syngenta.FWA.WebUI
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
(function () {
‘use strict‘;
angular
.module(‘fwa‘)
.factory(‘webapi‘, webapi);
webapi.$inject = [‘$http‘, ‘$httpParamSerializer‘];
function webapi($http, $httpParamSerializer) {
return {
post: function (className, actionName, parameters) {
return $http({
method: ‘POST‘,
url: ‘api/AppService/Post‘,
data: JSON.stringify({ Class: className, Action: actionName, Parameters: parameters })
});
},
getMenu: function () {
return $http({
method: ‘get‘,
url: ‘api/Security‘
});
},
postList: function (actionList) {
return $http({
method: ‘POST‘,
url: ‘api/AppService/PostActionList‘,
data: JSON.stringify(actionList)
});
}
}
}
})();
下一篇:C#之ProtoBuf应用基础