调用service,而Context.ServerUrl 为 null.
金蝶云社区-风雨起
风雨起
0人赞赏了该文章 1,262次浏览 未经作者许可,禁止转载编辑于2016年01月29日 15:43:40

建立插件类,继承AbstractListPlugIn,在插件中调用自己写的service,需要传递this.Context,而使用T service = _mapServer.GetService(typeof(T).AssemblyQualifiedName, url);时,因为其中的url是插件中的this.Context.ServerUrl,而ServerUrl为null,导致出错。我要在自己写的service中访问数据库。

问题是为什么ServerUrl会为null?
我自定义工厂已经继承ServiceFactory。
代码如下:

///


/// 应用服务器服务组件创建工厂
///

public class CustomerServiceFactory: ServiceFactory
{
///
/// 服务接口-实现类 映射关系
///

public static new ServicesContainer _mapServer = new ServicesContainer();

static CustomerServiceFactory()
{
RegisterService();
}
static bool notRegistered = true;
///


/// 获取指定接口实现类实例
///

///
///
///
public static new T GetService(Context ctx)
{
if (ctx == null)
{
throw new Exception("{ctx == null}");
}
return GetService(ctx, ctx.ServerUrl);
}
///
/// 在App层 通过该方法访问服务;请通过kingdee.bos.app.servicehelper访问
///

///
///
///
static T GetService(Context ctx, string url)
{
if (ctx == null)
{
throw new Exception("{ctx == null}");
}

return GetService(url);
}

static T GetService(string url)
{
if (notRegistered)
{
RegisterService();
}
var instance = _mapServer.GetService(typeof(T).AssemblyQualifiedName, url);
if (instance == null)
throw new KDException("???", "instance==null");
return instance;
}
///


/// 注册服务
///

public static new void RegisterService()
{
if (notRegistered)
{
_mapServer.Add(typeof(ICustomService).AssemblyQualifiedName, "CTC.DataSyncTask.ICustomService,CTC.DataSyncTask");

notRegistered = false;
}
}

public static ICustomService GetCustomService(Context ctx)
{
return GetService(ctx);
}

///


/// 销毁服务类实例
///

///
public static new void CloseService(Object service)
{
IDisposable disposable = service as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
}

您的鼓励与嘉奖将成为创作者们前进的动力,如果觉得本文还不错,可以给予作者创作打赏哦!

请选择打赏金币数 *

10金币20金币30金币40金币50金币60金币
可用金币: 0