Silverlight通过httpBinding访问IIS宿主WCF
silverlight和wcf通信是大家开发中用得相对较多的东西,我以Silverlight 通过 httpBinding 访问 IIS 宿主 WCF 来简单介绍一下。 Silverlight 通过 httpBiding方式 访问 IIS 宿主 WCF是我们在Silverlight与WCF通信中最为常见的,也是用的最多的,我们用个很简
silverlight和wcf通信是大家开发中用得相对较多的东西,我以Silverlight通过httpBinding访问IIS宿主WCF 来简单介绍一下。
Silverlight通过httpBiding方式访问IIS宿主WCF是我们在Silverlight与WCF通信中最为常见的,也是用的最多的,我们用个很简单的例子进行演示。
项目结构:
项目目结构简单说明:
程序集名称 | 需添加的引用 | 简要说明 |
LxContracts | System.Runtime.Serialization System.ServiceModel | 用于存放操作契约与数据契约 |
LxServices | LxContracts[项目] | 服务,操作契约的实现 |
WcfHost.web | LxContracts[项目] 和LxServices[项目] | 利用Svc文件发布服务的站点 |
SilverlightDemo | Silverlight程序,调用WCF服务 |
注意:建立Silverlight程序的时候,不需要承载网站,建立一个单一的Silverlight程序即可,这样做的原因是,把Silverlight和WCF服务不放到同一个站点下面,是为了演示跨域的问题。
代码实现:
类库LxContracts:(包括数据契约Student.cs和操作契约IStudent.cs)
Student.cs 代码
代码如下 | 复制代码 |
using<span> System; </span>using<span> System.Collections.Generic; </span>using<span> System.Linq; </span>using<span> System.Text; </span>using<span> System.ServiceModel; </span>using<span> System.Runtime.Serialization; </span>namespace<span> LxContracts { [DataContract] </span>public class<span> Student { </span>/// <summary> ///<span> 学生编号 </span>/// </summary> <span> [DataMember] </span>public int StuId { get; set<span>; } </span>/// <summary> ///<span> 学生姓名 </span>/// </summary> <span> [DataMember] </span>public string StuName { get; set<span>; } </span>/// <summary> ///<span> 所在班级 </span>/// </summary> <span> [DataMember] </span>public string ClassName { get; set<span>; } </span>/// <summary> ///<span> 联系电话 </span>/// </summary> <span> [DataMember] </span>public string TelPhoneNum { get; set<span>; } } }</span> Copy after login |
代码如下 | 复制代码 |
using<span> System; </span>using<span> System.Collections.Generic; </span>using<span> System.Linq; </span>using<span> System.Text; </span>using<span> System.Runtime.Serialization; </span>using<span> System.ServiceModel; </span>namespace<span> LxContracts { [ServiceContract] </span>public interface<span> IStudent { [OperationContract] List</span><student><span> GetStudent(); } }</span></student> Copy after login |
StudentList.cs
代码如下 | 复制代码 |
using<span> System; </span>using<span> System.Collections.Generic; </span>using<span> System.Linq; </span>using<span> System.Text; </span>using<span> LxContracts; </span>namespace<span> LxServices { </span>public class StudentList:List<student><span> { </span>public<span> StudentList() { </span>this.Add(new Student() { StuId = 1, StuName = "小明", ClassName = "计算机一班", TelPhoneNum = "123456"<span> }); </span>this.Add(new Student() { StuId = 2, StuName = "小红", ClassName = "计算机二班", TelPhoneNum = "234567"<span> }); </span>this.Add(new Student() { StuId = 2, StuName = "小兰", ClassName = "计算机三班", TelPhoneNum = "890123"<span> }); } } }</span></student> Copy after login |
代码如下 | 复制代码 |
using<span> System; </span>using<span> System.Collections.Generic; </span>using<span> System.Linq; </span>using<span> System.Text; </span>using<span> LxContracts; </span>namespace<span> LxServices { </span>public class<span> StudentService:IStudent { </span>public List<student><span> GetStudent() { </span>//<span>实际情况应该为从数据库读取 </span>//本例手动生成一个StudentList StudentList ListStuent = new<span> StudentList(); </span>return<span> ListStuent; } } }</span></student> Copy after login |
站点WcfHost.web,这是一个Asp.net 空web应用程序。
1、右击” WcfHost.web”—“添加”—“新建项”—“wcf服务”,命名为”StudentSrv.svc” 。如图:
在项目中删除”StudentSrv.svc.cs”文件和”IStudentSrv.cs”文件。右击”StudentSrv.svc”文件,选择”查看标记”,将代码修改为:
2、修改webconfig 文件,代码如下:
WebConfig
代码如下 | 复制代码 |
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetframework="4.0"></compilation> </system.web> <system.servicemodel> <behaviors> <servicebehaviors> <behavior name="LxBehavior"> <servicemetadata httpgetenabled="true"></servicemetadata> <servicedebug includeexceptiondetailinfaults="false"></servicedebug> </behavior> </servicebehaviors> </behaviors> <services> <service name="LxServices.StudentService" behaviorconfiguration="LxBehavior"> <endpoint address="" binding="basicHttpBinding" contract="LxContracts.IStudent"></endpoint> </service> </services> <!--关闭 ASP.NET 兼容性模式--> <servicehostingenvironment aspnetcompatibilityenabled="false"></servicehostingenvironment> </system.servicemodel> </configuration> Copy after login |
3、右击”StudentSrv.svc”文件,在”浏览器中查看”,显示如下图,说明服务已经部署好了,我用的端口是 9090:
在Silverlight中进行调用:
Silverlight调用wcf很简单,直接在”SilverlightDemo”中添加”服务引用即可”,Silverlight项目中会自动生成” ServiceReferences.ClientConfig”配置文件,当然也可以利用代码的方式调用,但是我比较懒 :)。
1、为Silverlight程序添加WCF:
“右击”—“SiverlightDemo”—“添加服务引用”—“输入服务地址”(我的是http://localhost:9090/WCF/StudentSrv.svc)--点击“前往”,就会找到服务,命名为“WCF.StudentSrv”后,点击“确定”
2、在Silverlight中调用WCF:
MainPage.xaml中添加”DataGrid”控件,xaml代码如下:
MainPage.xaml 代码
代码如下 | 复制代码 |
<datagrid x:name="dgStudnet" grid.row="0" autogeneratecolumns="False"> <datagrid.columns> <datagridtextcolumn header="学生编号" width="80" binding="{Binding StuId}"></datagridtextcolumn> <datagridtextcolumn header="学生姓名" width="100" binding="{Binding StuName}"></datagridtextcolumn> <datagridtextcolumn header="所在班级" width="120" binding="{Binding ClassName}"></datagridtextcolumn> <datagridtextcolumn header="电话号码" width="100" binding="{Binding TelPhoneNum}"></datagridtextcolumn> </datagrid.columns> </datagrid> Copy after login |
代码如下 | 复制代码 |
public partial class<span> MainPage : UserControl { ObservableCollection</span><student><span> listStudent; </span>public<span> MainPage() { InitializeComponent(); listStudent </span>= new ObservableCollection<student><span>(); </span>this.Loaded += new<span> RoutedEventHandler(MainPage_Loaded); } </span>void MainPage_Loaded(object<span> sender, RoutedEventArgs e) { StudentClient proxyClient </span>= new<span> StudentClient(); proxyClient.GetStudentAsync(); proxyClient.GetStudentCompleted </span>+= new EventHandler<getstudentcompletedeventargs><span>(proxyClient_GetStudentCompleted); } </span>void proxyClient_GetStudentCompleted(object<span> sender, GetStudentCompletedEventArgs e) { </span>if (e.Error == null<span>) { listStudent </span>=<span> e.Result; </span>this.dgStudnet.ItemsSource =<span> listStudent; } } }</span></getstudentcompletedeventargs></student></student> Copy after login
|
将” SilverlightDemo”设置为启动项目,运行,会产生下面的异常:
这就是因为当时建立项目的时候没有把Silverlight程序和WCF服务放到同一个站点的缘故,因此需要在发布WCF的网站根目录放置一个跨域文件:clientaccesspolicy.xml
clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8"?> <access-policy> <cross-domain-access> <policy> <allow-from http-request-headers="SOAPAction"> <domain uri="*"></domain> </allow-from> <grant-to> <resource path="/" include-subpaths="true"></resource> </grant-to> </policy> </cross-domain-access> </access-policy>
再次运行,结果如下图所示:
至此,Silverlight通过httbBingding方式访问IIS宿主的WCF的演示我们就进行到这里

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In iOS17, Apple has more control over what apps can see in photos. Read on to learn how to manage app access by app. In iOS, Apple's in-app photo picker lets you share specific photos with the app, while the rest of your photo library remains private. Apps must request access to your entire photo library, and you can choose to grant the following access to apps: Restricted Access – Apps can only see images that you can select, which you can do at any time in the app or by going to Settings > ;Privacy & Security>Photos to view selected images. Full access – App can view photos

A JsonNode is Jackson's JSON tree model that can read JSON into JsonNode instances and write JsonNode into JSON. We can use Jackson to read JSON into a JsonNode by creating an ObjectMapper instance and calling the readValue() method. We can access fields, arrays or nested objects using the get() method of the JsonNode class. We can use the asText() method to return a valid string representation and convert the node's value to Javaint using the asInt() method of the JsonNode class. In the example below we can access Json

We can access the metadata of audio files using Mutagen and the eyeD3 module in Python. For video metadata we can use movies and the OpenCV library in Python. Metadata is data that provides information about other data, such as audio and video data. Metadata for audio and video files includes file format, file resolution, file size, duration, bitrate, etc. By accessing this metadata, we can manage media more efficiently and analyze the metadata to obtain some useful information. In this article, we will take a look at some of the libraries or modules provided by Python for accessing metadata of audio and video files. Access audio metadata Some libraries for accessing audio file metadata are - using mutagenesis

How to solve the problem that Tomcat cannot successfully access the war package after deploying it requires specific code examples. As a widely used Java Web server, Tomcat allows developers to package their own developed Web applications into war files for deployment. However, sometimes we may encounter the problem of being unable to successfully access the war package after deploying it. This may be caused by incorrect configuration or other reasons. In this article, we'll provide some concrete code examples that address this dilemma. 1. Check Tomcat service

How to solve the problem of accessing and calling external resources in PHP development requires specific code examples. In PHP development, we often encounter situations where we need to access and call external resources, such as API interfaces, third-party libraries or other server resources. When dealing with these external resources, we need to consider how to access and call safely while ensuring performance and reliability. This article describes several common solutions and provides corresponding code examples. 1. Use the curl library to call external resources. Curl is a very powerful open source library.

How to solve the problem of access denied when modifying files in win7? When modifying some system files, we will often be prompted that we do not have permission to perform the operation. We can turn off the folder permissions or obtain administrator rights. For users who need to modify such files, let’s take a look at the following specific tutorials. Solution to the problem of access denied when modifying files in win7: 1. First select the corresponding folder, click the tool above, and select the folder option. 2. Enter the View tab. 3. Uncheck Use Simple File Sharing and confirm. 4. Then right-click the corresponding folder and click Properties. 5. Enter the Security tab. 6. Select the icon position and click Advanced. 7

Sharing folders is indeed an extremely useful feature in a home or business network environment. It allows you to easily share folders with other users, thereby facilitating file transfer and sharing. Win10 Home Edition shared folder cannot be accessed Solution: Solution 1: Check network connection and user permissions When trying to use Win10 shared folders, we first need to confirm whether the network connection and user permissions are normal. If there is a problem with the network connection or the user does not have permission to access the shared folder, it may result in inaccessibility. 1. First, please ensure that the network connection is smooth so that the computer and the computer where the shared folder is located are in the same LAN and can communicate normally. 2. Secondly check the user permissions to confirm that the current user has permission to share files.

There is no unique access to the camera device in iOS. The official specification recommendation is as follows - User agent implementations of this specification are recommended to ask for user consent before starting to capture content through the microphone or camera. This may be necessary to meet regulatory, legal and best practice requirements related to user data privacy. Additionally, user agent implementations are recommended to provide an indication to the user when an input device is enabled and to enable the user to terminate such capture. Likewise, user agents are recommended to provide user controls, such as allowing the user to select the exact media capture device to use if multiple devices are present. Disable sound capture in video capture mode.
