Служба WCF с ASP.Net

Я новичок в WCF.

Я написал следующий сервис ниже, который работает нормально. Затем я настроил IIS и запустил службу через браузер http://localhost/WCF_Entity_SVC/ProductService.svc, который работает нормально. Я создал веб-страницу ASP.Net и добавил ссылку на службу, используя URL-адрес. Когда я запускаю страницу ASP, я получаю внутреннюю ошибку. Любая помощь приветствуется.

public class ProductService : IProductService
    {
        public Product GetProduct(int id)
        {
            NorthwindEntities context = new NorthwindEntities();
            var productEntity = (from p
                                 in context.ProductEntities
                                 where p.ProductID == id
                                 select p).FirstOrDefault();
            if (productEntity != null)
                return TranslateProductEntityToProduct(productEntity);
            else
                throw new Exception("Invalid product id");
        }
        private Product TranslateProductEntityToProduct(
              ProductEntity productEntity)
        {
            Product product = new Product();
            product.ProductID = productEntity.ProductID;
            product.ProductName = productEntity.ProductName;
            product.QuantityPerUnit = productEntity.QuantityPerUnit;
            product.UnitPrice = (decimal)productEntity.UnitPrice;
            product.Discontinued = productEntity.Discontinued;
            return product;
        }
    }

ASP-код

 public partial class _Default : System.Web.UI.Page
    {
        myService.ProductServiceClient objService = new ProductServiceClient();

        protected void Page_Load(object sender, EventArgs e)
        {
           var results = objService.GetProduct(45);   //debug shows internal error
           dgResult.DataSource = results;
           dgResult.DataBind();

        }
    }

Добавление отладки к webconfig показывает следующую ошибку:

The underlying provider failed on Open. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

        Exception Details: System.ServiceModel.FaultException`1[[System.ServiceModel.ExceptionDetail, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]: The underlying provider failed on Open.

        Source Error: 


        Line 159:        
        Line 160:        public ASPWFC.myService.Product GetProduct(int id) {
        Line 161:            return base.Channel.GetProduct(id);
        Line 162:        }
        Line 163:    }

    Source File: D:\My Documents\Visual Studio 2010\Projects\wcf\WCF_Entity\ASPWFC\Service References\myService\Reference.cs    Line: 161 

    Stack Trace: 


    [FaultException`1: The underlying provider failed on Open.]
       System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +9464367
       System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +345
       ASPWFC.myService.IProductService.GetProduct(Int32 id) +0
       ASPWFC.myService.ProductServiceClient.GetProduct(Int32 id) in D:\My Documents\Visual Studio 2010\Projects\wcf\WCF_Entity\ASPWFC\Service References\myService\Reference.cs:161
       ASPWFC._Default.Page_Load(Object sender, EventArgs e) in D:\My Documents\Visual Studio 2010\Projects\wcf\WCF_Entity\ASPWFC\Default.aspx.cs:23
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +37
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +95
       System.Web.UI.Control.OnLoad(EventArgs e) +145
       System.Web.UI.Control.LoadRecursive() +134
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3412

В целях отладки я добавляю в службу функцию Getparams(int id), и все функции, которые она выполняет, возвращают id. Я вызвал функции Getparams на странице ASp и получил пропущенный аргумент. Я думаю, это означает, что параметр в функции GetProduct передается службе и где-то в ссылке на запрос сущности, это ошибка.

Чего я не понимаю, так это того, что когда я запускаю сервис напрямую, ошибки нет. База данных дает мне результат

Я сделал еще несколько отладок и обнаружил, что причина ошибки:

sqlError: Ошибка входа для IIS AppPool\ASP.Net4

Как мне исправить это, мой sqlserver запускается через проверку подлинности Windows.

Я нашел скрипт в msdn, который исправляет проблему безопасности, и код работает

CREATE LOGIN [IIS APPPOOL\ASP.NET v4.0] 
  FROM WINDOWS WITH DEFAULT_DATABASE=[Northwind], 
  DEFAULT_LANGUAGE=[us_english]
GO
CREATE USER [NWUser] 
  FOR LOGIN [IIS APPPOOL\ASP.NET v4.0]
GO
EXEC sp_addrolemember 'db_datareader', 'NWUser'
GO

1 ответ

Решение

При отладке служб WCF это действительно помогает включить трассировку. Это позволяет диагностировать расплывчатые сообщения ("500 Internal Server Error" на самом деле мало что дает) без лишних ошибок.

Зайдите сюда: http://msdn.microsoft.com/en-us/library/ms732023.aspx чтобы увидеть подробности. Короче добавлю

<system.diagnostics>
    <trace autoflush="true" />
    <sources>
            <source name="System.ServiceModel" 
                    switchValue="Information, ActivityTracing"
                    propagateActivity="true">
            <listeners>
               <add name="sdt" 
                   type="System.Diagnostics.XmlWriterTraceListener" 
                   initializeData= "c:\temp\wcfserver.log" />
            </listeners>
         </source>
    </sources>
</system.diagnostics>

вашему клиенту, вашему серверу или обоим; настроить имя файла журнала в initializeData по мере необходимости.

Запустите вашу программу, и вы должны увидеть один или два файла журнала. Откройте их с помощью Service Trace Viewer (часть Windows SDK, установленного в более поздних версиях Windows), и вы увидите исключение, выделенное красным.

Исключения, поднятые WCF, в частности внутренние исключения, обычно очень явные, так что это определенно стоит усилий.

(Это также работает для System.Runtime.Serialization который спас мой бекон пару раз в последнее время.)

Другие вопросы по тегам