【问题标题】:How to call a method in external dll referenced in WCF service project如何调用 WCF 服务项目中引用的外部 dll 中的方法
【发布时间】:2012-10-16 12:04:54
【问题描述】:

是否可以通过添加方法类的dll来调用我们WCF服务中的外部方法?

我有一个现有的 Web 应用程序项目,其方法为 getcabfare(**),现在我正在尝试通过将现有应用程序的引用添加到我的 WCF 项目来创建一个新的 WCF 服务来访问该 getcabfare() 方法。

但我得到了错误

用户代码未处理 NullReferenceException - 对象引用 未设置为对象的实例

这是我现有的项目“BasicCabApplication”,它在业务逻辑层具有以下方法,运行良好

namespace BasicCabApplication
{
 public class BussinessLogic
 {  
    public int getcabfare(string location)
    {
  string conn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;
     SqlConnection con = new SqlConnection(conn);
     con.Open();
     SqlCommand cmd = new SqlCommand("Select fare from cablocations where area ='" + location +"'", con);
     int fare = Convert.ToInt16(cmd.ExecuteScalar());
     con.Close();
     return fare;
  }
}

这是我添加了上述项目的引用的 wcf 项目。 Service1.svc.cs 中的代码如下

using BasicCabApplication;
namespace mywcftest1
{
   public class Service1 : IService1
   {
        public int GetFare(string location)
        {
           BussinessLogic bl = new BussinessLogic();
            int fare = bl.getcabfare(location);
            return fare;
        }
    }
}

我得到了对象 bl,但无法访问引发 nullreference 异常的方法 getcabfare()。

在 wcf 项目(web service) 中不能访问另一个项目的方法吗?或我的代码中的任何错误....

【问题讨论】:

标签: c# asp.net wcf


【解决方案1】:

你的问题在这里:

string conn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;

您的 wcf 项目在其配置文件中没有此配置,因此会引发 nullreference 异常。 您必须使用连接字符串将字符串添加到 wcf 服务的配置文件中,它将解决您的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多