Tag: WCF

  • WCF Configuration

    WCF Configuration

    During the deployment of a WCF service to an HTTPS QA server recently had us stumped… The service didn’t respond, didn’t display any error messages and didn’t log any issues in the Windows Event Log.

    The issues turned out to be (as expected) a type in the configuration file, we had used;

    system.serviceModel/services/service/endpoint/behaviorName

    and this should have been;

    system.serviceModel/services/service/endpoint/behaviorConfiguration

    Grrr!!!

  • MSDTC

    MSDTC

    We had some really strange problems getting MSDTC to work recently, specifically upgrading to VS2010 and SL4. Our developer environments are pointing to networked database servers and during this particular use-case the development workstation is running a both the hosted Silverlight Client and the Business Logic (API) served through a WCF service (both in Cassini). The API is performing the transactional logic.

    So, working through the bugs and error messages that we got;

    The first error requires MSDTC to be started (which is wasn’t by default):

    The transaction manager has disabled its support for remote/network transactions. Both the WCF Server (in our case the developer workstation) and the Database Server have to have MSDTC running. Start MSDTC using the command line: NET START MSDTC

    YMMV, but on Win7 the default properties for DTC also had to be updated. From Component Services MMC, navigate to Computers | My Computer | Distributed Transaction Coordinator | Local DTC. Right click + Properties and set as follows:

    clip_image002

    Next error then relates to the default firewall setting:

    The MSDTC transaction manager was unable to pull the transaction from the source transaction manager due to communication problems. Possible causes are: a firewall is present and it doesn’t have an exception for the MSDTC process, the two machines cannot find each other by their NetBIOS names, or the support for network transactions is not enabled for one of the two transaction managers. Sometimes we didn’t get this exception, and got a timeout instead and the failed transaction could be seen in the outstanding transactions list.

    To resolve this we updated the Windows Firewall to add an inbound and outbound tunnel for %SystemRoot%\System32\msdtc.exe

    All seems to OK … for now… :S

  • Silverlight 3, Validation, MVVM, WCF and EF

    All the current articles and tutorials on Silverlight 3 Validation show really simplistic examples, business objects in the UI space, no services etc etc

    My Problem

    When the business objects are being created and managed via the entity framework, there does not appear to be any simple ways of adding the validation properties to the object fields. This means that by the time the object gets to the UI, we have no control.

    in addition to this I need to be able to provide UI base validation and messaging based on user inputs. This has to happen on the field edit and also on “Save click”. Not as easy as it sounds.

    MY Workaround

    Anyway, I have a workaround that appears to be doing what I need. This involves a few more tweaks than I feel is appropriate, so I’d be interested to hear your thoughts on this approach. Anyway, our form is as follows:

    Previously we had a business object in the ViewModel (FocusedDepartment) and were binding the values of the textboxes to the fields of this object, for example:

    <TextBox x:Name="Txt_Number" Text="{Binding Mode=TwoWay, Path=FocusedDepartment.DepartmentId}" />

    The problem with this was the FocusedDepartment type (Department) was derived from the service layer, which in turn had come from our EF model. We did not find a solution to attach the validation properties to the EF model objects. The workaround for this was to add specific properties into the ViewModel to support the fields, and attach validation properties to these:

    This property still uses the FocusedDepartment property as a backing store. In the xaml we changed our binding to use this new property:

    <TextBox x:Name="Txt_Number" Text="{Binding Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True, Path=DepartmentId}" />

    The FocusedDepartment property also had to notify property changes for the new fields:

    //the currently selected item, bound to the datacontext of the view
    private Department _focusedDepartment = new Department();
    public Department FocusedDepartment
    {
    get { return _focusedDepartment; }
    set
    {
    _focusedDepartment = value;
    _OnPropertyChanged("FocusedDepartment");


    _OnPropertyChanged("Description");
    _OnPropertyChanged("DepartmentId");
    }
    }

    From this point we where able to see the validation on the text boxes appear correctly but only when the values in the textboxes had been changed.

    When tabbing through the fields, the bound fields where not getting touched, so the validation was not occurring. To workaround this issue I have added some code to the Save button click event (sorry MVVM purists!!) to update the source of the binding expressions, then checked the error count against the ViewModel before submitting the save action:

    Firstly, I have a handler to manage the error count in the VM

    Then handled the save button click in code behind

    This ensures the fields are valid (and the validation messages are displayed) before committing the Save action to the service.

    This approach (particularly the checking and setting of values directly in the VM) feels pretty dirty at the moment L, but it does seem to work for UI validation.

  • Persisted Properties

    Back in classic ASP.NET I’d persist data extract from a web service in base class property as follows:

    private string m_stringData;
    public string _stringData
    { get {
    if (m_stringData==null)
    {
    //fetch data from my web service
    m_stringData = ws.FetchData()
    }
    return m_stringData;
    }
    }

    This way I could always simple use _stringData and know that I’d always get the data I was after (sometimes I’d use Session state as a store instead of a private member variable).

    In Silverlight with a WCF this doesn’t work, because the WCF service has to be called asynchronously.

    So, how do I get round this?… There are a number of routes to take. One method involves wrapping the messages synchronously and can be found at codeplex

    Another alternative that is less verbose, and arguably easier, to follow is using a lambda expression as follows:

    private string m_stringData;
    public string _stringData
    {
    get
    { //if we don't have a list of departments, fetch from WCF
    if (m_stringData == null)
    {
    StringServiceClient client = new StringServiceClient();
    client.GetStringCompleted +=
    (sender, e) =>
    {
    m_stringData = e.Result;
    };
    client.GetStringAsync();
    }

    return m_stringData;
    }
    }

    A final solution was posted on this stackoverflow thread

  • Extracting values from ServiceReference.ClientConfig

    You can open the document as an XDocument and go from there:

    var streamInfo = Application.GetResourceStream(new Uri(“ServiceReferences.ClientConfig”, UriKind.Relative));
    var config = XDocument.Load(streamInfo.Stream);
    var endpoints = from endpoint in config.Descendants(“endpoint”)
    where endpoint.Attribute(“name”).Value == endPointName
    select new { Address = endpoint.Attribute(“address”).Value };
    foreach (var endpoint in endpoints)
    {
    if (_binding.Security.Mode == BasicHttpSecurityMode.Transport)
    {
    String address = endpoint.Address.Replace(“http://&#8221;, “https://&#8221;);
    //address = string.Concat(address, “/secure/”);
    result = new EndpointAddress(address);
    }
    else
    {
    result = new EndpointAddress(endpoint.Address);
    }
    }