【问题标题】:Not able to invoke the WCF function无法调用 WCF 函数
【发布时间】:2011-10-05 06:14:01
【问题描述】:

我有一个 asp .net 应用程序,我的数据访问层是一个 WCF 服务。我用的是VWD Express 2010。鸟瞰整个结构是这样的

    [ServiceContract]
    public interface IExcelReader
    {
       [OperationContract]
       [FaultContract(typeof(StaffAllocationFault))]        
       void ReadExcel();
    }


    public void ReadExcel()
    {
        DataSet dataCollection = new DataSet();

        table = new DataTable("Capacity");

        //gets the connection string for the excelsheet with the employee details
        //string strCon = ConfigurationManager.ConnectionStrings["capacityDB"].ConnectionString;
        string strCon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\CapacityDB.xlsx;Extended Properties=Excel 12.0";

        //gets the predefined filters in the application
        string[] filter = GetDefinedFilters();

        //creates the connection object
        oledbCon = new OleDbConnection(strCon);

        try
        {
            //opens the connection object
            openConnection();

            string strCmd = "Select * from [Capacity Report Data$]";

            //creates the command object
            oledbCmd = new OleDbCommand(strCmd, oledbCon);

            //fills the datatable using the data adapter
            oledbAdapter = new OleDbDataAdapter();
            oledbAdapter.SelectCommand = oledbCmd;
            oledbAdapter.Fill(table);

            dataCollection.Tables.Add(table);

            //to trim off the trailing/preceding whitespaces
            foreach (DataRow row in table.Rows)
            {
                int count = 0;

                while (count < filter.Count())
                {
                    try
                    {
                        row[filter[count]] = row[filter[count]].ToString().Trim();

                        //if the field is blank
                        if (row[filter[count]].ToString() == "")
                            row[filter[count]] = "***";

                        count++;
                    }

                    catch (Exception ex)
                    {
                        throw new FaultException<StaffAllocationFault>(new StaffAllocationFault { FaultMessage = "Error while reading through the employee information" }, ex.Message);
                    }
                }
            }                
        }

        catch (Exception ex)
        {
            throw new FaultException<StaffAllocationFault>(new StaffAllocationFault { FaultMessage = "Error while retreiving employee information" }, ex.Message);
        }

        finally
        {
            //closes the oledb connection
            closeConnection();
        }
    }


    public void ReadFromExcel()
    {
        try
        {
            new ExcelReaderClient().ReadExcel();
        }
        //service specific exceptions
        catch (FaultException<StaffAllocationFault> ex)
        {
            throw new ExceptionLayer.StaffAllocationException("Error while reading from excel", ex);
        }
        //generic exceptions
        catch (Exception genEx)
        {
            throw genEx;
        }
    }

我在 UI 中的web.config

<client>
        <endpoint address="http://localhost:49171/ExcelReader.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IExcelReader" contract="ExcelService.IExcelReader"
            name="BasicHttpBinding_IExcelReader" />
    </client>

当我运行应用程序时,wcf 中的函数没有被调用。请帮忙。

【问题讨论】:

  • 不...我没有收到任何错误 ;(
  • 您在 web.config 中配置了什么? WCF 需要指定其端点。
  • 我在 UI 的 web .config 中有这个.., localhost:49171/Simple.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISimple" contract="SimpleService.ISimple" name= "BasicHttpBinding_ISimple" />
  • 没有调用该方法并且您没有收到任何错误的建议似乎极不可能。您是否使用调试器单步执行了代码?如果是这样,当您到达new SimpleClient().Sample() 行并踏入其中时会发生什么?可能值得发布真正的代码,因为如果它的内容比您在此处发布的更多,那么您很可能错过了一些东西。
  • 我发现诊断此类问题的最佳方法是使用 WCF 跟踪。见这里:msdn.microsoft.com/en-us/library/ms733025.aspx

标签: c# asp.net wcf web-services wcf-binding


【解决方案1】:

在 Visual Studio 中为 IIS 中的 WCF 服务创建一个虚拟目录。之后从 VS 命令提示符调用 WcfTestClient,并尝试将 wcf 服务引用添加到 WCFTestClient(在您的情况下,url 可能类似于 http://localhost/VirtualDirectoryName/ExcelReader.svc) - 如果可以访问服务及其方法,您可以从工具 - 否则它会给你适当的错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多