vs2010+Silverlight4+wcf开发部署全过程

海涵德 2010-11-28 11:00:58
加精
1 安装开发环境
操作系统:Windows 7 专业版 微软先锋计划199元购
Vs2010:cn_visual_studio_2010_ultimate_x86_dvd_532347.iso这是微软送的一个版本,先前在微软下载的。
银光套件:Silverlight.exe
Silverlight4_Tools.exe
Silverlight_Developer.exe
2 安装
依次安装:vs2010,完全安装
Silverlight.exe
Silverlight4_Tools.exe
Silverlight_Developer.exe,按微软说的只要安装了Silverlight4_Tools.exe,就不用安装Silverlight_Developer.exe,但是在调试程序还是要我安装Silverlight_Developer,我就有安装了它。
3 建立一个vs2010+silverlight4+wcf的项目
为了少啰嗦,大家看图吧。

























silverlight项目中的脚本
UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
<StackPanel >
<TextBlock x:Name="text_result" Text="显示结果"></TextBlock>
<Button x:Name="button_get_welcome" Content="测试wcf" Width="100"
Click="button_get_welcome_Click" ></Button>
</StackPanel>
</Grid>
</UserControl>

后台代码:
Partial Public Class MainPage
Inherits UserControl

Private wcf As New ServiceReference1.Service1Client
Private Sub wcf_get_wcf_welcomeCompleted(ByVal s As Object, _
ByVal e As ServiceReference1.get_wcf_welcomeCompletedEventArgs)
text_result.Text = e.Result
End Sub

Public Sub New()
InitializeComponent()
AddHandler wcf.get_wcf_welcomeCompleted, AddressOf wcf_get_wcf_welcomeCompleted

End Sub

Private Sub button_get_welcome_Click(ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs)
wcf.get_wcf_welcomeAsync()

End Sub
End Class

wcf中添加的一段代码:
  <OperationContract()> Public Function get_wcf_welcome() As String
Return "欢迎连接wcf成功。"
End Function

这是一个部署和使用成功的项目,不好意思是vb代码,不过不影响整个过过程。
...全文
7808 200 打赏 收藏 转发到动态 举报
写回复
用AI写文章
200 条回复
切换为时间正序
请发表友善的回复…
发表回复
sokenhe 2012-10-19
  • 打赏
  • 举报
回复
学习了,哈哈
brightfran 2012-08-02
  • 打赏
  • 举报
回复
很好,学习
yangjian5210 2011-04-07
  • 打赏
  • 举报
回复
不错,男子汉都不用中文版的
w176236767 2011-03-30
  • 打赏
  • 举报
回复
楼主,Silverlight_Developer.exe有没有下载地址提供呀?
海涵德 2011-01-16
  • 打赏
  • 举报
回复
按照例子一模一样的做一个简单的wcf,应该没有什么问题。
huangyf168 2011-01-16
  • 打赏
  • 举报
回复
回楼上,我用的是win7+vs2010
海涵德 2011-01-15
  • 打赏
  • 举报
回复
你使用的是什么版本,iis?
huangyf168 2011-01-15
  • 打赏
  • 举报
回复
我按照楼主所说,引用wcf总是出现错误。错误信息如下
下载“http://localhost:3837/Service1.svc”时出错。
请求失败,错误信息为:
--
<html>
<head>
<title>无法在当前宿主环境下调用“ServiceHostingEnvironment.EnsureServiceAvailable”。此 API 要求调用应用程序承载于 IIS 或 WAS 中。</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Lucida Console";font-size: .9em}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
</style>
</head>

<body bgcolor="white">

<span><H1>“/”应用程序中的服务器错误。<hr width=100% size=1 color=silver></H1>

<h2> <i>无法在当前宿主环境下调用“ServiceHostingEnvironment.EnsureServiceAvailable”。此 API 要求调用应用程序承载于 IIS 或 WAS 中。</i> </h2></span>

<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

<b> 说明: </b>执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

<br><br>

<b> 异常详细信息: </b>System.InvalidOperationException: 无法在当前宿主环境下调用“ServiceHostingEnvironment.EnsureServiceAvailable”。此 API 要求调用应用程序承载于 IIS 或 WAS 中。<br><br>

<b>源错误:</b> <br><br>

<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code>

执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。</code>

</td>
</tr>
</table>

<br>

<b>堆栈跟踪:</b> <br><br>

<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>

[InvalidOperationException: 无法在当前宿主环境下调用“ServiceHostingEnvironment.EnsureServiceAvailable”。此 API 要求调用应用程序承载于 IIS 或 WAS 中。]
System.ServiceModel.ServiceHostingEnvironment.EnsureInitialized() +415
System.ServiceModel.ServiceHostingEnvironment.OnEnsureInitialized(Object state) +4
System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state) +52
System.ServiceModel.ServiceHostingEnvironment.SafeEnsureInitialized() +65
System.ServiceModel.Activation.HttpHandler.ProcessRequest(HttpContext context) +10
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +100
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
</pre></code>

</td>
</tr>
</table>

<br>

<hr width=100% size=1 color=silver>

<b>版本信息:</b> Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.1

</font>

</body>
</html>
<!--
[InvalidOperationException]: 无法在当前宿主环境下调用“ServiceHostingEnvironment.EnsureServiceAvailable”。此 API 要求调用应用程序承载于 IIS 或 WAS 中。
在 System.ServiceModel.ServiceHostingEnvironment.EnsureInitialized()
在 System.ServiceModel.ServiceHostingEnvironment.OnEnsureInitialized(Object state)
在 System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state)
在 System.ServiceModel.ServiceHostingEnvironment.SafeEnsureInitialized()
在 System.ServiceModel.Activation.HttpHandler.ProcessRequest(HttpContext context)
在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->
--.
元数据包含无法解析的引用:“http://localhost:3837/Service1.svc”。
响应消息的内容类型 text/html; charset=utf-8 与绑定(application/soap+xml; charset=utf-8)的内容类型不匹配。如果使用自定义编码器,请确保正确实现 IsContentTypeSupported 方法。响应的前 1024 个字节为:“<html>
<head>
<title>无法在当前宿主环境下调用“ServiceHostingEnvironment.EnsureServiceAvailable”。此 API 要求调用应用程序承载于 IIS 或 WAS 中。</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Lucida Console";font-size: .9em}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
</style>
</head>

<body bgcolor="white">

<span”。
远程服务器返回错误: (500) 内部服务器错误。
如果该服务已在当前解决方案中定义,请尝试生成该解决方案,然后再次添加服务引用。
diy2005 2011-01-05
  • 打赏
  • 举报
回复
楼主神通广大啊
sweety404 2010-12-16
  • 打赏
  • 举报
回复
endpoint地址之类的配置,能不能写的详细点呢,非常感谢
海涵德 2010-12-16
  • 打赏
  • 举报
回复
     Shared Function getEndpointAddress(ByVal relative_uri As String) As EndpointAddress
Return New EndpointAddress(New Uri(Application.Current.Host.Source, relative_uri))
End Function

Shared Sub use_wcf(ByVal wcf_client_instance As ServiceReferenceOpticalLine.ServiceOpticalLineClient, ByVal end_point_address As String)
Dim _end_point_address As EndpointAddress

_end_point_address = getEndpointAddress(end_point_address) '
' _end_point_address = getEndpointAddress("../Services/Service1.svc")

#If TRACE Then
' 调用wcf超时的解决办法
wcf_client_instance.Endpoint.Binding.OpenTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.CloseTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.SendTimeout = New TimeSpan(0, 18, 0)
wcf_client_instance.Endpoint.Binding.ReceiveTimeout = New TimeSpan(0, 18, 0)
#Else
wcf_client_instance.Endpoint.Binding.OpenTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.CloseTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.SendTimeout = New TimeSpan(0, 5, 0)
wcf_client_instance.Endpoint.Binding.ReceiveTimeout = New TimeSpan(0, 5, 0)
#End If

wcf_client_instance.Endpoint.Address = _end_point_address


End Sub
海涵德 2010-12-16
  • 打赏
  • 举报
回复
Shared Function getEndpointAddress(ByVal relative_uri As String) As EndpointAddress
Return New EndpointAddress(New Uri(Application.Current.Host.Source, relative_uri))
End Function

Shared Sub use_wcf(ByVal wcf_client_instance As ServiceReferenceOpticalLine.ServiceOpticalLineClient, ByVal end_point_address As String)
'Dim _basic_http_binding As BasicHttpBinding
Dim _end_point_address As EndpointAddress

'_basic_http_binding = getBasicHttpBinding()
_end_point_address = ClassesOptialLineShared.getEndpointAddress(end_point_address) ' _end_point_address = ClassesOptialLineShared.getEndpointAddress("../Services/Service1.svc")

#If TRACE Then
' 调用wcf超时的解决办法
wcf_client_instance.Endpoint.Binding.OpenTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.CloseTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.SendTimeout = New TimeSpan(0, 18, 0)
wcf_client_instance.Endpoint.Binding.ReceiveTimeout = New TimeSpan(0, 18, 0)
#Else
wcf_client_instance.Endpoint.Binding.OpenTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.CloseTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.SendTimeout = New TimeSpan(0, 5, 0)
wcf_client_instance.Endpoint.Binding.ReceiveTimeout = New TimeSpan(0, 5, 0)
#End If

wcf_client_instance.Endpoint.Address = _end_point_address


End Sub
海涵德 2010-12-16
  • 打赏
  • 举报
回复
[Quote=引用 183 楼 sweety404 的回复:]
endpoint地址之类的配置,能不能写的详细点呢,非常感谢
[/Quote]
对于iis7 什么也不用配置,对于iis6需要配置:
   Shared Function getEndpointAddress(ByVal relative_uri As String) As EndpointAddress
Return New EndpointAddress(New Uri(Application.Current.Host.Source, relative_uri))
End Function

Shared Sub use_wcf(ByVal wcf_client_instance As ServiceReferenceOpticalLine.ServiceOpticalLineClient, ByVal end_point_address As String)
'Dim _basic_http_binding As BasicHttpBinding
Dim _end_point_address As EndpointAddress

'_basic_http_binding = ClassesOptialLineShared.getBasicHttpBinding()
_end_point_address = ClassesOptialLineShared.getEndpointAddress(end_point_address) ' _end_point_address = ClassesOptialLineShared.getEndpointAddress("../Services/Service1.svc")

#If TRACE Then
' 调用wcf超时的解决办法
wcf_client_instance.Endpoint.Binding.OpenTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.CloseTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.SendTimeout = New TimeSpan(0, 18, 0)
wcf_client_instance.Endpoint.Binding.ReceiveTimeout = New TimeSpan(0, 18, 0)
#Else
wcf_client_instance.Endpoint.Binding.OpenTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.CloseTimeout = New TimeSpan(0, 1, 0)
wcf_client_instance.Endpoint.Binding.SendTimeout = New TimeSpan(0, 5, 0)
wcf_client_instance.Endpoint.Binding.ReceiveTimeout = New TimeSpan(0, 5, 0)
#End If

wcf_client_instance.Endpoint.Address = _end_point_address


End Sub
lyc5305562 2010-12-16
  • 打赏
  • 举报
回复
好的很啊 。。。
Richard345265669 2010-12-10
  • 打赏
  • 举报
回复
弱弱的问一句,你以上的操作中的"打开或关闭Windows”功能之后的操作,我的系统是XP SP3的,找不到对应的内容啊??
xh7530215 2010-12-07
  • 打赏
  • 举报
回复
楼主你强大啊
Richard345265669 2010-12-07
  • 打赏
  • 举报
回复
AddHandler wcf.get_wcf_welcomeCompleted, AddressOf wcf_get_wcf_welcomeCompleted

这两句什么意思,用C#如何改写??
abuying 2010-12-07
  • 打赏
  • 举报
回复
谢谢分享!
wocaniyan 2010-12-07
  • 打赏
  • 举报
回复
界面很漂亮啊……
海涵德 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 178 楼 richard345265669 的回复:]
AddHandler wcf.get_wcf_welcomeCompleted, AddressOf wcf_get_wcf_welcomeCompleted

这两句什么意思,用C#如何改写??
[/Quote] wcf.get_wcf_welcomeCompleted += wcf_get_wcf_welcomeCompleted;
加载更多回复(115)

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧