/* by josh */

2014年3月26日 星期三

[C#] .NET Compact Framework 3.5之WCF 使用說明

微軟已在 .net compact framework 3.5版中加入了WCF client的功能(但並未支援WCF server);
要在.net compact framework上使用wcf service必須用到Power Toys for .NET Compact Framework 3.5中的ServiceModel Metadata Tool for the .NET Compact Framework (NetCFSvcUtil)程式來產生Proxies藉以呼叫WCF service。
注意:若你是使用Windows 7以上的OS開發,務必下載支援windows 7的NetCFSvcUtil,否則無法正確產生proxies

以下說明如何建立service及client,

Server端:
Step 1: 新增合約IGreetingService.cs,此例之合約名稱為IGreetingService

namespace WcfDemoService
{
 // 注意: 若變更此處的介面名稱 "IGreetingService",也必須更新 App.config 中 "IGreetingService" 的參考。

    [ServiceContract]
    public interface IGreetingService
    {
        [OperationContract]
        string DoWork();
              
    }
}

Step 2:實作合約內容GreetingService.cs


namespace WcfDemoService
{
    // 注意: 若變更此處的類別名稱 "GreetingService",也必須更新 App.config 中 "GreetingService" 的參考。
    public class GreetingService : IGreetingService
    {
        public string DoWork()
        {
            return "This is return string2";
        }
      
    }
}
Step 3: 在主程式中撰寫開啟服務的程式碼
 class Program
 {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(GreetingService));
            try
            {
                host.Open();
                Console.WriteLine("Server Opened !");
                Console.Read();
            }
            finally
            {
                if (host.State == CommunicationState.Faulted)
                    host.Abort();
                else
                    host.Close();
            }
        }
 }
Step 4: 建立App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WcfDemoService.GreetingServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="WcfDemoService.GreetingServiceBehavior"
                name="WcfDemoService.GreetingService">
                <endpoint address="GreetingService" binding="basicHttpBinding" contract="WcfDemoService.IGreetingService" bindingConfiguration="basicHttpBindingConfiguration">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://192.168.0.103:8731"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
      <bindings>
        <basicHttpBinding>
          <binding name="basicHttpBindingConfiguration" maxReceivedMessageSize="20971510">
            <readerQuotas maxStringContentLength="20971520" maxArrayLength="20971520"/>
         
          </binding>
        </basicHttpBinding>
      </bindings>
    </system.serviceModel>
</configuration>

Client端:
Step 1: 使用NetCFSvcUtil產生Proxy檔案,方法如下:
    a) 先執行host程式
    b)在cmd下cd到NetCFSvcUtil.exe的路徑下,輸入NetCFSvcUtil.exe http://host ip:host port/  按          下enter後會產生兩個檔案GreetingService.csCFClientBase.cs
        

Step 2:建立.net compact framework專案並匯入GreetingService.csCFClientBase.cs

Step 3: 在程式中撰寫呼叫wcf service的程式碼
private void btnGetStringByWCF_Click(object sender, EventArgs e)
{
System.ServiceModel.Channels.Binding binding = 
GreetingServiceClient.CreateDefaultBinding();
   string address = GreetingServiceClient.EndpointAddress.Uri.ToString();
   GreetingServiceClient m_proxy = 
new GreetingServiceClient(binding, new System.ServiceModel.EndpointAddress(address));
   
string GetString=m_proxy.DoWork();
  this.textBox1.Text = GetString;
}

2014年2月24日 星期一

[C#] 使用LinQ連接MS SQL Server發生error 26

當使用LinQ連接資料庫時發生Error 26時(如下圖)

此時請檢查Web,Config (App.Config) 檔案,並且確認Connection String為何,然後至 dbml的design.cs中找尋LinQ所使用的Connect string與Web.Config中相同
    1.) 確認.Config中設定了哪些DB連線及其名稱:

   2.)檢查dbml的design.cs中所使用的DB連線

    上圖LinQ所使用的connection string為ConnectionString3,在.Config中就必須有ConnectString3的設定且必須是可以連接到的DB

2014年1月8日 星期三

2013年9月10日 星期二

[Visual Studio] 加入WCF服務參考時產生錯誤"無法為服務參考xxx產生程式碼"或加入參考時"建置系統已經參考這個元件"


When you add Service Reference into Silverlight project, some time you get “failed to generate code for the service reference…” error.  Which means Visual Studio is failed to generate client code, and if see “ServiceReferences.ClientConfig” file it is empty. This is the common error most of the people get.

Step 1. In "Add Service Reference", Get in to "Advance"
 
 
 
Step 2. uncheck the “Reuse types in referenced assemblies” check box and click on Ok button.
 
Once you click on Ok button in Service Reference dialog box, the client code will be generated and you will not get any error. Now if you open “ServiceReferences.ClientConfig” file you can see the generated code.


2013年8月5日 星期一

[C#] asp.net加入Silverlight的WCF service (可用於連接MSSQL)

Silverlight連接 MS SQL必須透過WCF;

Step1:
首先必須在asp.net 網頁中加入WCF Service:


Step2:
撰寫合約內容:

Step3: 
於Silverlight中加入服務參考

Step4:
於Silverlight xaml.cs中使用WCF Service,需注意的是這邊的WCF服務必須使用非同步的方式呼叫(1.先定義服務完成的事件處理函式2.設計事件處理函式內容3.呼叫服務)

2012年12月27日 星期四

[網路] 使用net use指令連接網路硬碟

假設遠端目錄為\\1.1.1.100\Data\  登入之帳號/密碼為Administrator/Password
若愈在本機電腦新增一網路硬碟(Y:)  連接至遠端目錄,則可於命令提示字元中輸入以下指令:

net use Y:  \\1.1.1.100\Data  user:Administrator "Password"

可將此指令寫於批次檔中,如此可達到開機自動連線的目的

2012年11月27日 星期二

[MS SQL] 予許遠端連線 MS SQL server 之防火牆設定

若SQL Server Configuration Manager (組態管理員) 已開啟1433 port,但仍無法遠端連線,通常是因為被防火牆擋掉了,此時只須將sqlserver.exe加入防火牆例外清單即可。方法如下:


使用控制台中的 Windows 防火牆項目,將程式例外加入至防火牆

  1. 在 [控制台] 中,於 [Windows 防火牆] 項目的 [例外] 索引標籤上,按一下 [新增程式]
  2. 瀏覽至您想要通過防火牆的 SQL Server,例如 C:\Program Files\Microsoft SQL Server\MSSQL11.<instance_name>\MSSQL\Binn,選取 sqlservr.exe,然後按一下 [開啟]
  3. 按一下 [確定]。