ASP.Net 的 WCF 服务
我是 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。我在 ASp 页面中调用了 Getparams 函数,并返回了传递的参数。我认为这意味着 GetProduct 函数中的参数正在传递给服务,并且实体查询链接中的某处是错误。
我不明白的是为什么当我直接运行服务时没有错误。数据库给了我一个结果,
我做了一些更多的调试,我发现打开错误的原因是:
sqlError: Login failed for 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
I am new to WCF.
I wrote the following service below, which runs fine. I then configured IIS, and ran the service via the browser http://localhost/WCF_Entity_SVC/ProductService.svc, which runs fine. I created a ASP.Net webpage and added a service refrence using the url. When I run the ASP page, I recieve an internal error. Any help is appreciated.
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 code
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();
}
}
Adding Debuging to the webconfig shows the below error:
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
For debugging purposes, I add a funcction called Getparams(int id) to the service
all the function does is return the id. I called the Getparams functions in ASp page, and got back the argument passsed. I think that means that the param in the GetProduct function is being passed to the service and somewhere in the link to entity query, is the error.
What I don't understand is why when I run the service directly, there is no error. The database gives me a result
I did some more debuging, and I found the reason for the open error is:
sqlError: Login failed for IIS AppPool\ASP.Net4
how do I go about fixing this, my sqlserver runs via windows authentication.
I found a script in msdn that fixes the security issue, and the code is know working
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调试 WCF 服务时,启用跟踪确实很有帮助。这可以让您诊断模糊的消息(“500 内部服务器错误”并没有真正泄露太多信息),而不会造成太多混乱。
转到此处:http://msdn.microsoft.com/en-us/library/ ms732023.aspx 查看详细信息。简而言之,添加
到您的客户端、服务器或两者;根据需要调整
initializeData
中的日志文件名。运行您的程序,您应该会看到一两个日志文件。使用服务跟踪查看器(Windows SDK 的一部分,随更高版本的 Windows 一起安装)打开它们,您应该看到以红色突出显示的异常。
WCF 引发的异常,特别是内部异常,通常非常明确,因此这绝对值得付出努力。
(这也适用于 System.Runtime.Serialization,它最近救了我几次。)
When debugging WCF services it really helps to enable tracing. This lets you diagnose vague messages ("500 Internal Server Error" doesn't really give much away) without too much kerfuffle.
Go here: http://msdn.microsoft.com/en-us/library/ms732023.aspx to see the detail. In brief, add
to your client, your server, or both; adjust the log filename in
initializeData
as appropriate.Run your program, and you should see a log file or two. Open them with the Service Trace Viewer (part of the Windows SDK, installed with later versions of Windows) and you should see the exception highlighted in red.
The exceptions raised by WCF, in particular the inner exceptions, are usually very explicit, so this is definitely worth the effort.
(This also works for
System.Runtime.Serialization
which has saved my bacon a couple of times recently.)